我正在尝试为我的集成测试中的网络获取运行 Jest 测试。它使用 beforeEach 创建了一个虚假的网络获取响应,它应该返回一个长度为 2 的响应。当我从以下代码中删除 done 时,测试运行良好,但是只要我用作done
回调并且似乎测试失败并且错误表明只返回了 1 的长度,而预期的长度应该是 2。
它使用 Enzyme 和 full dom 来测试 3 个组件的集成,我不使用的时候测试很好done
,但是我一使用done
它就失败了。
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.