Jest configuration
Explanations about how Jest is configured by Next Right Now, and how you can extend the default configuration.
Table of contents
jest-extended
We extended jest with various additional assertions.
See https://github.com/jest-community/jest-extended
jest-expect-message
We extended jest to add custom messages to Jest expects.
See https://www.npmjs.com/package/jest-expect-message
How is Jest configured?
The jest.config.js contains all the Jest configuration, including:
- TypeScript’s configuration (so that Jest can run
.tsfiles)- A dedicated
tsconfig.jest.jsonis used when running TS files (which inherits from thejest.config.jsonconfiguration)
- A dedicated
- Built-in extra configuration, using
setupFilesAfterEnv:jest-extended(see above)jest-expect-message(see above)jest.setup.js, which configure Jest globally, such as:- Imports
.env.localand.envfiles (.env.localtakes precedence when env variable clash) - Imports Next.js polyfills (such as
fetch) - Provides utilities such as
muteConsole,muteConsoleButLogandunmuteConsolefor logging
- Imports
jest.extends.js, which extends Jestexpectfunction globally, with:toContainObjecttoMatchOneOffrom jest-to-match-shape-oftoMatchShapeOffrom jest-to-match-shape-of
- Built-in extra configuration, using a custom
runnerwithjest-runner-groups:- It allows you to run a bunch of test together (AKA a “group”).
- This is very useful to run, or exclude a group of files from a test run.
- A few groups already exists, such as
api,components,integration,unit,utilsand have a dedicated NPM script (e.g:yarn test:group:unit) - For instance, the tests executed by Vercel when deploying a new domain do not run the
integrationtests, as they take a long time to run, and too often fail due to network, or I/O-related false-positive issues.
How to extend Jest?
- If you wish to change the Jest configuration globally, you should do so in
jest.setup.js. - If you wish to extend the Jest configuration to enhance the
expectfunction, you can either:- Do so in
jest.config.jsonatsetupFilesAfterEnv, if the library is meant to be configured in such a way (such asjest-extended). - Alternatively, do so in
jest.extends.js.
- Do so in