1.0.0 • Published 3 years ago
@lottiefiles/jest-config v1.0.0
@lottiefiles/jest-config
Shared configuration for jest.
Table of contents
Installation
- Install the @lottiefiles/jest-config to the project with pnpm.
pnpm add -D @lottiefiles/jest-config- Create a
jest.config.cjsfile in the project with the contents:
const { createConfig } = require('@lottiefiles/jest-config');
const packageJson = require('./package.json');
module.exports = {
...createConfig(),
name: packageJson.name,
displayName: packageJson.name,
};Install Dependencies
The following must be installed as a dev dependency in your project.
Peer Dependencies
| Package Name | Version |
|---|---|
| esbuild | * |
| jest | * |
Configuration
Monorepos
The recommended setup for a monorepo project is:
- Install
jestandesbuildas a workspaces root dev dependency.
pnpm add -DW jest @types/jest esbuild- Create a
jest.config.cjsin the workspace root.
module.exports = {
// Setup projects
projects: ['<rootDir>/packages/*'],
};- Install
@lottiefiles/jest-configas a project dev dependency in each of the projects you are setting upjestfor.
pnpm add -D @lottiefiles/jest-config- Create a
jest.config.cjsfile in each of the projects.
const { createConfig } = require('@lottiefiles/jest-config');
const packageJson = require('./package.json');
module.exports = {
...createConfig(),
name: packageJson.name,
displayName: packageJson.name,
};You can then customize this file to fit the needs of each project individually.
ESM projects
- Pass in the transform format and transform target arguments to the
createConfigfunction to configurejestfor processing and treating the files as ESM.
...createConfig({
transformFormat: 'esm',
transformTarget: 'es2020'
}),- Make sure you invoke jest with running
nodewith ES module support. You can do this by setting the NODE_OPTIONS environment variable to--experimental-vm-modules.
For e.g., the "test" script command can be:
{
// ...
"scripts": {
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest"
}
}Tips and Tricks
Mocking objects and functions, with dynamic return values
Recording HTTP calls
Use PollyJS
Running heavy and long running tasks under forked processes in parallel
Use jest-worker
Creating spies for classes
Use jest-auto-spies
Starting a server before running tests
Use jest-dev-server
Running tests all in a browser environment
Use Playwright Test instead of Jest. See playwright.dev
Changelog
See CHANGELOG.md for the latest changes.
1.0.0
3 years ago