Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
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:

module.exports = {
    getSomething: async () => {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve("");
            }, 1000);
        });
    },
    doSomething: async () => {
        const data = await module.exports.getSomething();
        if (!data) {
            return module.exports.restart();
        }
        return data;
    },
    restart: async () => {
        return module.exports.doSomething();
    }
};

myCode.test.js file:

const myCode = require("./exampleCode")

describe("test", () => {
    test("Is it doing something more than once?", async () => {
        const restartSpy = jest.spyOn(myCode, 'restart');
        myCode.doSomething()
        expect(restartSpy).toHaveBeenCalledTimes(1);
    })
})

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The main problem here is the lack of await before myCode.doSomething(). All your functions are asynchronous, so you need to wait for them to finish before checking the spy:

await myCode.doSomething();

Another issue is the fact that it's an infinite recursion loop - jest will timeout after 5000ms (by default) if you won't modify the code that is calling restart, for example:

doSomething: async (restartCounter = 0) => {
    const data = await module.exports.getSomething();
    if (!data && ++restartCounter < 2) {
        return module.exports.restart(restartCounter);
    }
    return data;
},
restart: async (restartCounter) => {
    return module.exports.doSomething(restartCounter);
}
about 4 years ago · Juan Pablo Isaza Report

0

Actually, I've found a workaround.

describe("test", () => {
    test("Is it doing something more than once?", async () => {
        myCode.restart = jest.fn()
        const restartSpy = jest.spyOn(myCode, 'restart');
        await myCode.doSomething()
        expect(restartSpy).toHaveBeenCalledTimes(1);
    })
})

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!