Jest is a popular and delightful JavaScript testing framework known for its simplicity and comprehensive features, widely adopted for testing React, Vue, Angular, Node.js, and other JavaScript projects. The current stable version is 30.3.0. While major releases historically had long gaps (v30 came three years after v29), the project aims for more frequent major releases moving forward. Key differentiators include its "zero-config" setup for many projects, powerful snapshot testing capabilities for UI and data structures, an interactive watch mode for efficient TDD, built-in code coverage reporting, and a rich ecosystem of matchers and extensions. It ships with a custom JSDOM environment for browser-like testing in Node.js, making it suitable for front-end applications.
npm install jestVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic Jest test suite for a utility function, including setup with `describe` and `test`, using `expect` matchers, and a simple example of `jest.mock` for dependency mocking.
Ensure your development and CI environments use Node.js version 18.14.0 or higher.
Update deprecated matcher names to their canonical forms (e.g., `toBeCalled()` to `toHaveBeenCalled()`). An ESLint plugin with autofixers (`eslint-plugin-jest`) or codemods like `jest30-matcher-upgrade` can automate this.
Upgrade your project's TypeScript dependency to version 5.4 or newer.
Migrate to using only Jest's public APIs and documented interfaces. If you were using internal modules, check if a public alternative exists or open an issue/PR to request one.
For modules with dynamic mocks, wrap the module import in a function that is called after `jest.mock()`, or use `require()` for the module under test if `jest.mock()` is defined after top-level `import` statements.
Consider running tests within Windows Subsystem for Linux (WSL2) for improved performance on Windows machines. Optimize module imports to avoid large 'barrel files' that cause Jest to load unnecessary dependencies.
Ensure your test file is executed by Jest, and if using TypeScript or explicit imports, add `import { expect, test, describe } from '@jest/globals';` to the top of your test file.If your project uses ES Modules, ensure your `package.json` has `"type": "module"` and configure Jest to handle ESM, potentially with a Babel or `ts-jest` setup that targets ESM. If using CJS, stick to `require()` syntax or configure Jest to transpile ESM to CJS.
For `async/await` tests, simply mark the `test` callback as `async`. For promise-based tests, return the promise. Avoid mixing `done` with `async/await` or returning promises.
Move `jest.mock()` calls to the top-level of your test file or immediately inside `describe` blocks to allow Jest's auto-hoisting mechanism to work correctly.
Install `ts-jest` as a dev dependency (`npm install --save-dev ts-jest`) and ensure your Jest config's `preset` or `transform` property correctly points to `ts-jest`.