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!
 
     
    