I have a function that awaits multiple promises
const function = async () => {
await function1()
await function2()
await function3()
}
I want to test that function3 is called:
it(('calls function3', async () => {
jest.spyOn(api, 'function1').mockResolvedValue({})
jest.spyOn(api, 'function2').mockResolvedValue({})
spy = jest.spyOn(api, 'function3')
await function()
expect(spy).toBeCalledTimes(1)
})
and this test fails, but when I call await a lot of times:
it(('calls function3', async () => {
jest.spyOn(api, 'function1').mockResolvedValue({})
jest.spyOn(api, 'function2').mockResolvedValue({})
spy = jest.spyOn(api, 'function3')
await await await await await function()
expect(spy).toBeCalledTimes(1)
})
the test will pass. Why is this? Shouldn't await function() resolve all of the promises before moving onto the next expect line?
edit: The deeper the awaited function is i.e. a function4, the more await statements I need, but its not 1 to 1.