gpt4 book ai didi

typescript - 开 Jest 描述套件失败但独立运行测试成功

转载 作者:行者123 更新时间:2023-12-04 09:55:21 26 4
gpt4 key购买 nike

当我单独运行每个测试时,它们都成功了。但是当我通过 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);
})
})

测试被剪切以显示问题。正如你所看到的,他们几乎是平等的,每个人都有自己的 spy 。只是 axiosSpy 的返回值不同。所以我不能把它放在 before each .

当我通过 npm test 运行它们时,为什么第二个测试失败了?

最佳答案

您可能应该在 beforeEach 中重置您的模拟?我使用类似下面的代码:

beforeEach(() => jest.resetAllMocks())

关于typescript - 开 Jest 描述套件失败但独立运行测试成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61931202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com