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

341
Views
How to test a function that returns an arrow function with Jest?

I have a function like this:

export const myFunc = (a, b ,callBack) => {
    const timer = setTimeout(() => {
        callBack(a+b);
    }, 10);
    return () => clearTimeout(timer);
};

I'm able to test the callback function just fine by doing this:

jest.useFakeTimers();

describe('myFunc', () => {
    const callBack = jest.fn();

    it('runs myFunc', () => {
        myFunc(1, 2, callBack);
        jest.runAllTimers();

        expect(callBack).toHaveBeenCalledWith(3);
    });

However, the last line return () => clearTimeout(timer) is never tested somehow. Jest report just says this line function not covered and statement not covered. Can anyone please tell me a way to cover this line without using any other external testing library like enzyme?

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

0

The function isn't called, so it's not covered.

Another test would be:

    let cleanup = myFunc(1, 2, callBack);
    cleanup()
    jest.runAllTimers();
    expect(callBack).not.toHaveBeenCalled();
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!