I am trying to run a Jest test for a network fetching in my integration test. It used beforeEach to create a fake network fetching response, and it should return a response of length 2. The test goes well when I remove the done from the following code, however as long as I used done
as the callback and it seems failed the test and the error suggest there is only length of 1 returned, while the expected length should be 2.
It's using Enzyme and full dom for testing the integration of 3 components, and the test went well when I don't use the done
, but as soon as I used done
it failed the test.
beforeEach(() => {
moxios.install();
moxios.stubRequest('http://jsonplaceholder.typicode.com/comments', {
status: 200,
response: [ { name: 'Fetch #1' }, { name: 'Fetch #2' } ]
});
});
afterEach(() => {
moxios.uninstall();
});
it('can fetch a list of comments and display them', (done) => {
// Attempt to render the *entire* app
const wrapped = mount(
<Root>
<App />
</Root>
);
// find the 'fetchComments' button and click it
wrapped.find('.fetch_comments').simulate('click');
// setTimeout is used because moxio introduces a little delay fetching the data.
// so setTimeout makes Jest to have a little delay so it won't throw error.
// Expect to find a list of comments!
//
setTimeout(() => {
wrapped.update();
console.log(wrapped.find('li').length);
expect(wrapped.find('li').length).toEqual(2);
wrapped.unmount();
done();
}, 3300);
});
1
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Uncaught [Error: expect(received).toEqual(expected) // deep equality
Expected: 2
Received: 1]
at reportException (/Users/dmml/Documents/Developer/reactPractice/testing/testing-stephen-grider/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:66:24)
at Timeout.callback [as _onTimeout] (/Users/dmml/Documents/Developer/reactPractice/testing/testing-stephen-grider/node_modules/jsdom/lib/jsdom/browser/Window.js:680:7)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7) JestAssertionError: expect(received).toEqual(expected) // deep equality
Expected: 2
Received: 1
at toEqual (/Users/dmml/Documents/Developer/reactPractice/testing/testing-stephen-grider/src/__tests__/integrations.test.js:36:37)
at Timeout.callback [as _onTimeout] (/Users/dmml/Documents/Developer/reactPractice/testing/testing-stephen-grider/node_modules/jsdom/lib/jsdom/browser/Window.js:678:19)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7) {
matcherResult: {
actual: 1,
expected: 2,
message: [Function],
name: 'toEqual',
pass: false
}
}
● can fetch a list of comments and display them
expect(received).toEqual(expected) // deep equality
Expected: 2
Received: 1
34 | wrapped.update();
35 | console.log(wrapped.find('li').length);
> 36 | expect(wrapped.find('li').length).toEqual(2);
| ^
37 |
38 | wrapped.unmount();
39 | done();
at toEqual (src/__tests__/integrations.test.js:36:37)
at Timeout.callback [as _onTimeout] (node_modules/jsdom/lib/jsdom/browser/Window.js:678:19)
Test Suites: 1 failed, 2 passed, 3 total
Tests: 1 failed, 5 passed, 6 total
Snapshots: 0 total
Time: 5.028s
Ran all test suites related to changed files.