Skip to content
Advertisement

Node.js and Jest: Testing promise loop, count how many times function has been called

I want to test my code using JEST, but I’m having some issues. I want to check, if restart() function has been called.

My code works like this, it’s waiting for the data, and if there’s no data it’s calling the same function again. Basically something like a loop.

myCode.js file:

JavaScript

myCode.test.js file:

JavaScript

My problem is that expect(restartSpy).toHaveBeenCalledTimes(1); is returning false.

The question is – what I’m doing wrong? Is there a way to test this code?

Advertisement

Answer

Actually, I’ve found a workaround.

JavaScript

I’m overwriting restart() function. So now, I’m able to add await to doSomething() function and it will no longer be inifinite loop. Now I can check if the restart function has been called

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement