Jest
Jest is a delightful JavaScript testing framework designed to ensure the correctness of any JavaScript codebase. As of version 30.3.0, it offers powerful features like snapshot testing, isolated environments, and a comprehensive assertion library. While major releases historically had longer intervals (v30 after three years), the project aims for more frequent major updates, alongside regular minor and patch releases.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Jest is attempting to run ES modules in a CommonJS context without proper configuration.fixEnsure your `package.json` has `"type": "module"` or configure Babel/TypeScript to transpile ES modules, and Jest to recognize your test files as modules. -
ReferenceError: describe is not defined
cause Jest's global test functions (describe, it, expect) are not available in the current scope.fixEnsure Jest is correctly installed and configured, and that you're running your tests via the `jest` command (e.g., `npx jest`) rather than executing test files directly. -
Jest encountered an unexpected token '<' (or similar)
cause Jest's transformer (often Babel or ts-jest) is not correctly configured to process modern JavaScript syntax, TypeScript, or JSX/TSX.fixVerify your `jest.config.js` includes the correct `transform` configuration for your project's syntax, e.g., `'^.+\.(ts|tsx)$': 'ts-jest'` or a Babel setup. -
Error: Jest: a "jest-environment-jsdom" package is required
cause Your Jest configuration specifies `testEnvironment: 'jsdom'` but the `jest-environment-jsdom` package is not installed.fixInstall the required environment: `npm install --save-dev jest-environment-jsdom` or `yarn add --dev jest-environment-jsdom`. -
Cannot find module './my-module' from 'my-test.js'
cause Jest's module resolver cannot find a specified module, often due to custom path aliases or non-standard module locations.fixConfigure `moduleNameMapper` in your `jest.config.js` to map module paths, especially for aliases (e.g., `"^@/(.*)$": "<rootDir>/src/$1"`).
Warnings
- breaking Jest v30.0.0 is a major release after three years, introducing a substantial number of changes that may require following the migration guide for existing projects.
- breaking Jest 30 requires Node.js v18.14.0, v20.0.0, v22.0.0, or >=v24.0.0. Older Node.js versions are no longer supported.
- breaking The `jest-runner` package was deprecated and removed starting from Jest 30.0.1.
- gotcha Previously, `unstable_mockModule` could have issues when attempting to mock `node:` prefixed core modules.
Install
-
npm install jest -
yarn add jest -
pnpm add jest
Imports
- defineConfig
import { defineConfig } from 'jest'; - expect
import { expect } from '@jest/globals';
Quickstart
// sum.ts
export function sum(a: number, b: number): number {
return a + b;
}
// sum.test.ts
import { sum } from './sum';
describe('sum', () => {
it('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
});
// To run: `npx jest sum.test.ts`