I'm having trouble using spyOn on the second and third calls of a function in my jest test
I attempted to follow the documentation with this approach:
it("should succeed after retry on first attempt failure", async () => {
jest.spyOn(nukiService, "lockDoors")
.mockResolvedValueOnce(Promise.resolve({ status: 503 }))
.mockResolvedValueOnce(Promise.resolve({ status: 200 }));
expect((await nukiService.lockDoorsWithRetry()).status)
.toBe(200);
});
it("should fail after 3 retries", async () => {
jest.spyOn(nukiService, "lockDoors")
.mockResolvedValueOnce(Promise.resolve({ status: 503 }))
.mockResolvedValueOnce(Promise.resolve({ status: 503 }))
.mockResolvedValueOnce(Promise.resolve({ status: 503 }));
expect((await nukiService.lockDoorsWithRetry()).status)
.toBe(424);
});
However, my test is failing due to timeout issues which I suspect are related to the spyOn not working as expected:
● NukiService › handle doors › should succeed after retry on first attempt failure
thrown: "Exceeded timeout of 5000 ms for a test.
Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout."
85 | });
86 |
> 87 | it("should succeed after retry on first attempt failure", async () => {
| ^
88 | jest.spyOn(nukiService, "lockDoors")
89 | .mockResolvedValueOnce(Promise.resolve({ status: 503 }))
90 | .mockResolvedValueOnce(Promise.resolve({ status: 200 }));
at services/nuki.service.spec.ts:87:5
at services/nuki.service.spec.ts:18:3
at Object.<anonymous> (services/nuki.service.spec.ts:5:1)