test: Add flushPromises utility function

This commit is contained in:
Joachim Van Herwegen
2022-03-31 12:07:02 +02:00
parent 76548011f2
commit 16e9368734
7 changed files with 40 additions and 28 deletions

View File

@@ -44,6 +44,17 @@ export function describeIf(envFlag: string, name: string, fn: () => void): void
return enabled ? describe(name, fn) : describe.skip(name, fn);
}
/**
* This is needed when you want to wait for all promises to resolve.
* Also works when using jest.useFakeTimers().
* For more details see the links below
* - https://github.com/facebook/jest/issues/2157
* - https://stackoverflow.com/questions/52177631/jest-timer-and-promise-dont-work-well-settimeout-and-async-function
*/
export async function flushPromises(): Promise<void> {
return new Promise(jest.requireActual('timers').setImmediate);
}
/**
* Mocks (some) functions of the fs system library.
* It is important that you call `jest.mock('fs');` in your test file before calling this!!!