Skip to content

Commit 46732e0

Browse files
committed
doc: fix the ESM mocks section
1 parent 6e8ea34 commit 46732e0

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,16 @@ Just use `"test": "exodus-test"`
168168

169169
- `--test-force-exit` — force exit after tests are done
170170

171-
## Jest compatibility
171+
## Module mocking in ESM
172172

173-
The `--jest` mode is mostly compatible with Jest. There are some noteworthy differences though.
174-
This tool does not hoist mocks, so it is important that a mock is defined before the module that uses it is imported.
175-
In ESM, this can be achieved with dynamic imports:
173+
Module mocks in ESM is a common source of confusion, as Jest in most old setups does not run real ESM,
174+
and instead uses Babel to transform ESM into CJS, and then hoists mocks on top of `require()` calls.
175+
176+
That hoisting is not possible in ESM world, as import statements are always resolved before any other code.
177+
178+
Also see [Jest documentation](https://jestjs.io/docs/ecmascript-modules#module-mocking-in-esm) on that.
179+
180+
To port code from CJS or Babel, e.g. the following approach with dynamic imports can be used:
176181

177182
```js
178183
jest.doMock('./hogwarts.js', () => ({

0 commit comments

Comments
 (0)