{"id":11137,"library":"jest-cli","title":"Jest Testing Framework CLI","description":"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.","status":"active","version":"30.3.0","language":"javascript","source_language":"en","source_url":"https://github.com/jestjs/jest","tags":["javascript","ava","babel","coverage","easy","expect","facebook","immersive","instant","typescript"],"install":[{"cmd":"npm install jest-cli","lang":"bash","label":"npm"},{"cmd":"yarn add jest-cli","lang":"bash","label":"yarn"},{"cmd":"pnpm add jest-cli","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used for desktop notifications when tests pass or fail, providing immediate feedback.","package":"node-notifier","optional":true}],"imports":[{"note":"Introduced in Jest 30.3.0 for type-safe configuration files (e.g., jest.config.ts). This is the recommended way to define Jest configurations when using TypeScript or ESM.","wrong":"const config = require('jest-config').defineConfig;","symbol":"defineConfig","correct":"import { defineConfig } from 'jest-config';"},{"note":"While `jest` is often available globally in test environments, explicitly importing from `@jest/globals` is recommended for better type safety and clarity, especially in ESM projects or when `globals: false` is set in config.","wrong":"import jest from 'jest'; // This imports the CLI, not the globals","symbol":"jest","correct":"import { jest } from '@jest/globals';"},{"note":"Similar to `jest`, `expect` is typically available globally. Explicitly importing from `@jest/globals` ensures type inference and is good practice, especially when `globals: false` is configured.","wrong":"import { expect } from 'jest-expect'; // Not the primary way","symbol":"expect","correct":"import { expect } from '@jest/globals';"}],"quickstart":{"code":"import { describe, expect, test } from '@jest/globals';\n\nfunction sum(a: number, b: number): number {\n  return a + b;\n}\n\ndescribe('sum module', () => {\n  test('adds 1 + 2 to equal 3', () => {\n    expect(sum(1, 2)).toBe(3);\n  });\n\n  test('object assignment', () => {\n    const data = { one: 1 };\n    data['two'] = 2;\n    expect(data).toEqual({ one: 1, two: 2 });\n  });\n\n  test('null', () => {\n    const n = null;\n    expect(n).toBeNull();\n    expect(n).toBeDefined();\n    expect(n).not.toBeUndefined();\n    expect(n).not.toBeTruthy();\n    expect(n).toBeFalsy();\n  });\n\n  test('truthy or falsy', () => {\n    const z = 0;\n    const a = 1;\n    expect(z).not.toBeTruthy();\n    expect(a).toBeTruthy();\n    expect(z).toBeFalsy();\n  });\n});\n\n// To run this test:\n// 1. Install Jest: npm install --save-dev jest typescript ts-jest @types/jest\n// 2. Create a jest.config.ts:\n//    import { defineConfig } from 'jest-config';\n//    export default defineConfig({\n//      preset: 'ts-jest',\n//      testEnvironment: 'node',\n//    });\n// 3. Add to package.json scripts: \"test\": \"jest\"\n// 4. Run: npm test\n","lang":"typescript","description":"Demonstrates basic Jest test structure using `describe`, `test`, and `expect` with TypeScript, along with setup instructions for a minimal Jest project."},"warnings":[{"fix":"Review the official Jest 30 migration guide (jestjs.io/docs/upgrading-to-jest30) and adapt test files, configurations, and dependencies as needed. Pay close attention to changes in custom resolvers, environments, or reporters.","message":"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.","severity":"breaking","affected_versions":">=30.0.0"},{"fix":"Upgrade your Node.js environment to version 18.14.0, 20.x, 22.x, 24.x or later. Use a tool like `nvm` to manage Node.js versions if necessary.","message":"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.","severity":"breaking","affected_versions":">=30.0.0"},{"fix":"Most users will not need to do anything. If you have highly customized test environments or module loading, verify that your setup still functions correctly. If issues arise, consult the Jest documentation on custom environments or module resolution.","message":"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.","severity":"deprecated","affected_versions":">=30.0.0-beta.4"},{"fix":"Explicitly import globals: `import { test, expect, describe } from '@jest/globals';` in each test file. Alternatively, ensure `globals: true` is set in your Jest configuration file.","message":"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.","severity":"gotcha","affected_versions":">=27.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Add `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.","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.","error":"ReferenceError: test is not defined"},{"fix":"Ensure 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.","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.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Verify 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', () => {...})`.","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.","error":"Your test suite must contain at least one test. (no tests found)"}],"ecosystem":"npm"}