I'm trying to understand how asynchronous testing works in Jest.
What I'm trying to do is similar to an example from the Jest documentation. This works fine ..
function doAsync(c) {
c(true)
}
test('doAsync calls both callbacks', () => {
expect.assertions(2);
function callback1(data) {
expect(data).toBeTruthy();
}
function callback2(data) {
expect(data).toBeTruthy();
}
doAsync(callback1);
doAsync(callback2);
});
But I want to delay the callback invocations so I tried this ....
function doAsync(c) {
setTimeout(() => {
console.log('timeout fired')
c(true)
}, 1000)
}
but the test fails with the message Expected two assertions to be called but received zero assertion calls.
.
The log message 'timeout fired' doesn't appear in the console.
Please can someone explain why it fails?
jest.useFakeTimers()
needs to be used by every test instead of being declared in the upper scope. – Nyctaginaceous