【发布时间】:2020-05-21 09:18:52
【问题描述】:
当我单独运行每个测试时,它们都成功了。但是当我通过npm test 一起运行它们时,第二个测试失败了:
Expected number of calls: 2
Received number of calls: 4
我有以下代码:(短而删节)
describe('checkDonations', () => {
test('it should call twice (test 1)', async () => {
const axiosSpy = jest.spyOn(axios.default, 'get')
.mockImplementation(() => {
return new Promise(resolve => {
resolve({
data: {
status: [
{
"created": "2020-04-08 21:20:17",
"timestamp": "1586373617",
"statusName": "new"
}
]
}
})
})
});
await checkDonations(null, {});
expect(axiosSpy).toHaveBeenCalledTimes(2);
})
test('it should call twice (test 2)', async () => {
const axiosSpy = jest.spyOn(axios.default, 'get')
.mockImplementation(() => {
return new Promise(resolve => {
resolve({
data: {
status: [
{
"created": "2020-04-08 21:20:17",
"timestamp": "1586373617",
"statusName": "final_success"
}
]
}
})
})
});
await checkDonations(null, {});
expect(axiosSpy).toHaveBeenCalledTimes(2);
})
})
测试被剪切以显示问题。如您所见,它们几乎相等,并且每个人都有自己的间谍常量。只是 axiosSpy 的返回值不同。所以我不能把它放在before each。
为什么我通过npm test 运行第二个测试失败了?
【问题讨论】:
标签: typescript jestjs