I am testing an API that will return objects of customer data (id, name age etc).
These objects will have some shared data depending on my query params like location etc. with the rest being dynamic. How can I best test the returned data is what I want?
example data - Say I have searched for all customers in a particular town:
{
"location": "some town",
"data": [
{ "id": "123", "name": "Bob" },
{ "id": "234", "name": "Jane" }
]
}
Of course for each different query param town I enter, there will be different data within the data array and of differing lengths.
I am using Jest and am wondering what is the best way to test?
So far I have the following:
test('customer data location', async () => {
const expect = require('./customer-obj.json');
const response = await getcustomersin('some town');
expect(response.body).toMatchObject(expected);
expect(response.body.data).toHaveLength(2);
});
Where customer-obj.json is the JSON file:
{
"location": "some town",
}
I used toMatchObject with this, but I feel that I am missing the point with not including the data values...
I did see expect.arrayContaining(array) on the official documentation, but I wouldn't know the data to include in this, as it seems to only work when you provide a subset of the overall array of data.
Aucun commentaire:
Enregistrer un commentaire