Skip to content Skip to sidebar Skip to footer

How To Test An Embedded Async Call

I am testing a function parent() that calls a function slomo() that returns a promise. I want to check that the code within the .then() is being executed. function slomo() { retu

Solution 1:

You correctly found the root case of the problem, that test finishes earlier then async code, so test should track async correctly. But seems to you lost the promise b/c you don't return it, so:

functionparent() {
  returnslomo() // <<<--- add return 
    .then((result) => {
      child()
    })
}

Next you simply can add done:

it('runs child', (done) => { // <<<< --- add doneparent().then(() => {
    expect(child).toHaveBeenCalledTimes(1)
    done(); // <<<< -- done should be called after all
  })
});

or just return the promise:

it('runs child', () => {
  returnparent().then(() => {  // <<<--- returnexpect(child).toHaveBeenCalledTimes(1);
  })
})

Hope it helps.

Post a Comment for "How To Test An Embedded Async Call"