Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: clarify mocking differences with jest #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@ Just use `"test": "exodus-test"`

- `--test-force-exit` -- force exit after tests are done (useful in integration tests where it could be unfeasible to resolve all open handles)

## Jest compatibility

The `--jest` mode is mostly compatible with Jest. There are some noteworthy differences though.
This tool does not hoist mocks, so it is important that a mock is defined before the module that uses it is imported.
In ESM, this can be achieved with dynamic imports:

```js
jest.mock('./hogwarts.js', () => {
return {
__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)
Loading