Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock an async function's delay time using Jest

I'm trying to write a test in nodeJS using Jest for a function that calls two async functions one after the other. I want to delay funcA, expect funcB to not be called, then run the timer down and expect funcB to be called.

The code looks something like this

//module1.js
async function mainFunc() {
  await module2.funcA()
  await module2.funcB()
}



//module2.js
async function funcA(){
  // making some async operation
}

async function funcB(){
  // making some async operation
}

I've tryed mocking the implementation of funcA like this:

const spyOnFuncA = jest.spyOn(module2, 'funcA').mockImplementation(async () => new Promise((r) => setTimeout(r, 1000)))

then in the test doing something like this:

  test('Should not call second function until first function resolved', async () => {
    jest.useFakeTimers()
    const spyOnFuncA = jest.spyOn(module2, 'funcA').mockImplementation(async () => new Promise((r) => setTimeout(r, 1000)))
    const spyOnFuncB = jest.spyOn(module2, 'funcB').mockImplementation()

    mainFunc()
    expect(spyOnFuncA).toBeCalled()
    expect(spyOnFuncB).not.toBeCalled()
    
    jest.runAllTimers()
    expect(spyOnFuncB).toBeCalled()
    
  })

I think the problem here is that the jest.useFakeTimers contradicts with the setTimeout inside the mockImplementation

Any ideas how should I test this?

Would appreciate any idea

Cheers!

like image 398
tosh Avatar asked Oct 18 '25 09:10

tosh


1 Answers

Mock funcA to return a deferred promise, to be resolved later. I know sinon provides a promise helper to cover deferred so jest may include a similar construct. Otherwise here is one of the simple implementations from that answer:

class Deferred {
  constructor() {
    this.promise = new Promise((resolve, reject) => {
      this.reject = reject
      this.resolve = resolve
    })
  }
}

Then the mock is something like:

    const deferred = new Deferred()
    const spyOnFuncA = jest.spyOn(module2, 'funcA').mockImplementation(() => deferred.promise)
    mainFunc() // avoid uncaught exceptions with `.catch`
      .catch(err => expect(err).toBe(null))
    expect(spyOnFuncA).toBeCalled()
    expect(spyOnFuncB).not.toBeCalled()
    await deferred.resolve('whatever')
    expect(spyOnFuncB).toBeCalled()
like image 89
Matt Avatar answered Oct 19 '25 23:10

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!