1 回答
TA贡献1796条经验 获得超4个赞
测试在Promise回调运行之前完成,所以它通过了......然后错误被记录下来。
如果测试包含异步代码,则您需要使用done:
test('diffFileFetch', done => { // <= use done
fetch.mockImplementation(() => new Promise((resolve, reject) => {
resolve('wrong value');
}));
targetFile.diffFileFetch()
.then(data => {
expect(data).toBe('correct value');
done(); // <= now call done
})
});
...返回Promise:
test('diffFileFetch', () => {
fetch.mockImplementation(() => new Promise((resolve, reject) => {
resolve('wrong value');
}));
return targetFile.diffFileFetch() // <= return the Promise
.then(data => {
expect(data).toBe('correct value');
})
});
...或者使用async测试功能和await在Promise:
test('diffFileFetch', async () => { // <= async test function
fetch.mockImplementation(() => new Promise((resolve, reject) => {
resolve('wrong value');
}));
const data = await targetFile.diffFileFetch() // <= await the Promise
expect(data).toBe('correct value');
});
添加回答
举报