Tatiana
09/30/2022, 11:50 AMeachLike matcher in my case, I checked several examples and messages in this chat but I still don’t see the problem.
My API should return something like:
{
"items": [
{
"name": "test event 1",
"id": "1cb9eb9e",
"teamId": "dummy_tid",
},
{
"name": "test event 2",
"id": "1cb9eb8a",
"teamId": "dummy_tid",
}
],
"totalCount": 2
}
The original response is much longer but my consumer uses only these fields.
The consumer tests looks like:
const EXISTING_EVENT_DATA = {
"name": "test event 1",
"id": "1cb9eb9e",
"teamId": "dummy_tid",
};
function createWrapper({ children }: { children: React.ReactElement }) {
return <Provider store={makeStore()}>{children}</Provider>;
}
const provider = new PactV3({
dir: path.resolve(process.cwd(), 'contract-tests/pacts'),
consumer: 'consumer',
provider: 'provider',
port: 8001,
});
describe('Event queries', () => {
it('should return all events', async () => {
provider
.given('I can get all events')
.uponReceiving('a request to get all events')
.withRequest({
method: 'GET',
path: `/events`,
query: { limit: '10', offset: '0', name: '' }
})
.willRespondWith({
status: 200,
body: {
items: eachLike(EXISTING_EVENT_DATA), totalCount: like(1)
},
});
return provider.executeTest(async () => {
const REQUEST_PARAMS = { limit: 10, offset: 0, name: '' };
const { result, waitForNextUpdate } = renderHook( () => useGetEventsQuery(REQUEST_PARAMS), {
wrapper: createWrapper,
});
await waitForNextUpdate();
expect(result.current.data).toEqual({items: [EXISTING_EVENT_DATA], totalCount: 1});
});
});
});
I am trying to use eachLike matcher here because I can have a lot of elements in an array.
The contract is generated.
But when I am trying to run the contract test agains my provider I am having errors:
Diff
--------------------------------------
Key: - is expected
+ is actual
Matching keys and values are not shown
{
"items": [
... ,
- Pact::UnexpectedIndex,
+ Hash,
]
}
Description of differences
--------------------------------------
* Actual array is too long and should not contain a Hash at $.items[1]
Such test implementation works if I have just one record in my DB, but if I have more it returns the error above.
Could you please help me to understand what this error means and what I am doing wrong?