Jest Testing Framework CLI
Jest is a popular and delightful JavaScript testing framework known for its focus on simplicity, performance, and features like snapshot testing and built-in mocking. It offers a powerful command-line interface (CLI) to run tests, generate coverage reports, and interact with the test runner. The current stable version is 30.3.0, released after a significant three-year development cycle for version 30.0.0, with a stated aim for more frequent major releases going forward. Jest differentiates itself with an 'all-in-one' approach, providing an assertion library, test runner, and mocking capabilities out of the box, often requiring minimal configuration for many projects. It supports TypeScript out-of-the-box and is widely adopted across various JavaScript environments, including Node.js, React, Angular, and Vue projects.
Common errors
-
ReferenceError: test is not defined
cause Jest's test globals are not available in the current scope. This typically happens if `globals: false` is configured in `jest.config.js` or if you are trying to run a test file without Jest's environment.fixAdd `import { test, expect, describe } from '@jest/globals';` at the top of your test file, or ensure `globals: true` is set in your Jest configuration file. -
SyntaxError: Cannot use import statement outside a module
cause This error occurs when Jest tries to process an ES module using CommonJS resolution rules, or vice versa, often due to misconfiguration of `type: 'module'` in `package.json` or incorrect `jest.config.js` settings for module resolution.fixEnsure your `jest.config.js` is correctly configured for your project's module system. If your project is ESM, ensure `type: 'module'` in `package.json` and use `jest.config.mjs` or `jest.config.ts` with `defineConfig`. If CJS, use `jest.config.js` and consider `transform` options for ESM imports. -
Your test suite must contain at least one test. (no tests found)
cause Jest couldn't find any test files matching its `testMatch` or `testRegex` configuration, or the test files themselves do not contain any `test` or `it` blocks.fixVerify your `jest.config.js` `testMatch` or `testRegex` patterns are correct (e.g., `**/*.test.ts`, `**/*.spec.js`). Ensure your test files contain actual test definitions using `test('description', () => {...})` or `it('description', () => {...})`.
Warnings
- breaking Jest 30 is a major release with significant internal refactors and changes. While many tests might work without modification, a migration guide is provided, and specific behaviors related to resolvers, runners, and environments may have changed. Always consult the official migration guide when upgrading from Jest 29 or earlier.
- breaking Node.js version requirements have been updated. Jest 30 now requires Node.js version 18.14.0 or higher. Running Jest with older Node.js versions will result in errors.
- deprecated The `jest-runtime` package no longer supports `new Script` and now uses `compileFunction`. While largely an internal change, this might affect projects that heavily customized module loading or relied on specific `new Script` behaviors within their Jest environment.
- gotcha Jest's globals (`jest`, `expect`, `test`, `describe` etc.) are enabled by default. If you use ESM or TypeScript and prefer explicit imports for better module hygiene and type inference, you might encounter 'is not defined' errors when `globals: false` is set in your config.
Install
-
npm install jest-cli -
yarn add jest-cli -
pnpm add jest-cli
Imports
- defineConfig
const config = require('jest-config').defineConfig;import { defineConfig } from 'jest-config'; - jest
import jest from 'jest'; // This imports the CLI, not the globals
import { jest } from '@jest/globals'; - expect
import { expect } from 'jest-expect'; // Not the primary wayimport { expect } from '@jest/globals';
Quickstart
import { describe, expect, test } from '@jest/globals';
function sum(a: number, b: number): number {
return a + b;
}
describe('sum module', () => {
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
test('object assignment', () => {
const data = { one: 1 };
data['two'] = 2;
expect(data).toEqual({ one: 1, two: 2 });
});
test('null', () => {
const n = null;
expect(n).toBeNull();
expect(n).toBeDefined();
expect(n).not.toBeUndefined();
expect(n).not.toBeTruthy();
expect(n).toBeFalsy();
});
test('truthy or falsy', () => {
const z = 0;
const a = 1;
expect(z).not.toBeTruthy();
expect(a).toBeTruthy();
expect(z).toBeFalsy();
});
});
// To run this test:
// 1. Install Jest: npm install --save-dev jest typescript ts-jest @types/jest
// 2. Create a jest.config.ts:
// import { defineConfig } from 'jest-config';
// export default defineConfig({
// preset: 'ts-jest',
// testEnvironment: 'node',
// });
// 3. Add to package.json scripts: "test": "jest"
// 4. Run: npm test