✅ tests/jest/timers.async.test.js
✔ PASS advanceTimersByTime() does not let microtasks to pass (5.326604ms)
✔ PASS advanceTimersByTime() does not let microtasks to pass even with await (1.336064ms)
✔ PASS advanceTimersByTimeAsync() lets microtasks to pass (6.99526ms)
✔ PASS advanceTimersByTimeAsync() lets microtasks to pass, chained (10.131664ms)
✔ PASS advanceTimersByTimeAsync() lets microtasks to pass, longer chained (8.635472ms)
✔ PASS advanceTimersByTimeAsync() lets microtasks to pass, async chain (56.937983ms)
See live output in [CI](https://github.com/ExodusOSS/test/actions/workflows/checks.yaml)
## Module mocking in ESM
Module mocks in ESM is a common source of confusion, as Jest in most old setups does not run real ESM,
and instead uses Babel to transform ESM into CJS, and then hoists mocks on top of `require()` calls.
That hoisting is not possible in ESM world, as static import statements are always resolved before
any other code.
Also see [Jest documentation](https://jestjs.io/docs/ecmascript-modules#module-mocking-in-esm) on that.
To port code from CJS or Babel, e.g. the following approach with dynamic imports can be used:
```js
jest.doMock('./hogwarts.js', () => ({
__esModule: true,
default: jest.fn(),
}))
const { default: getEntryQualification } = await import('./hogwarts.js')
const { qualifiesForHogwarts } = await import('./wizard.js') // module importing ./hogwarts.js
test('qualifies for Hogwarts', () => {
// doSomething is a mock function
getEntryQualification.mockReturnValue(['lumos'])
expect(qualifiesForHogwarts('potter')).toBe(false)
getEntryQualification.mockReturnValue([])
expect(qualifiesForHogwarts('potter')).toBe(true)
})
```
Note that all modules that transitively import `hogwarts.js` will have to be imported after the mock is defined.
## License
[MIT](./LICENSE)