Jest 02 ๐ Installation โ npm install -D jest & the jest –init Removal in v30
Jest installs as a dev dependency and, for most projects, needs no configuration file to run a test. The installation is one command, and the first test runs against the built-in defaults. The one change that trips up developers following older tutorials is in Jest 30: the jest --init command that scaffolded a configuration file was removed, and the replacement is npm init jest@latest (or the equivalent for Yarn and pnpm) . The install itself did not change; the way a project generates its config did.
This chapter covers the install command, the version requirements, the jest --init removal and its replacement, and the configuration file that a project may or may not need.
Key point: npm install -D jest is the install; Jest 30 removed the jest --init command, so a project that needs a config file runs npm init jest@latest or writes jest.config.js by hand.
a. The install command and version requirements
Jest is installed as a development dependency. The package manager variants are the standard ones: npm install --save-dev jest, yarn add --dev jest, and pnpm add --save-dev jest .
npm install --save-dev jest
Jest 30, released in June 2025, dropped support for older Node versions. The minimum is Node 18+, and the dropped lines are Node 14, 16, 19, and 21 . A project on an older Node version needs to upgrade before the install will work. TypeScript projects that use Jest’s types need TypeScript 5.4 or newer .
The install does not create a configuration file. A project that follows Jest’s conventionsโtest files named *.test.js or *.spec.js, in the default locationsโruns without one. The package.json script is the only setup most projects need:
{
"scripts": {
"test": "jest"
}
}
b. The jest --init removal in v30
The jest --init command was the interactive scaffolder that generated a jest.config.js file with prompts for the test environment, coverage, and other settings. Jest 30 removed it . The command no longer exists, and a script or tutorial that runs jest --init will fail with an unknown-command error.
The replacement is the package-manager init convention:
# npm
npm init jest@latest
# Yarn
yarn create jest
# pnpm
pnpm create jest
The npm init jest@latest command runs the scaffolder that jest --init used to run, and it is the documented replacement . A project that cannot use the interactive command writes jest.config.js by hand; the configuration reference documents the options, and the file is optional for a project that follows the defaults.
The removal is part of a broader Jest 30 cleanup. The release also removed long-deprecated matcher aliases (toThrowError โ toThrow, toReturnWith โ toHaveReturnedWith), renamed --testPathPattern to --testPathPatterns, and changed the jsdom environment to a more spec-compliant version that breaks some window.location mocking . The jest --init removal is the change that affects installation and setup rather than test code.
c. The configuration file and when it is needed
A jest.config.js (or .ts, .mjs, .cjs) file is optional. It is needed when the project deviates from the conventions: a non-standard test file location, a custom transformer for TypeScript or JSX, a different test environment, or coverage settings that differ from the defaults.
// jest.config.js
/** @type {import('jest').Config} */
module.exports = {
testEnvironment: "jsdom",
testMatch: ["**/__tests__/**/*.js", "**/?(*.)+(spec|test).js"],
collectCoverageFrom: ["src/**/*.js"],
};
Jest 30 supports TypeScript configuration files (.ts, .mts, .cts) and added .mts and .cts to the default moduleFileExtensions and testMatch patterns, so a TypeScript ESM project can use a jest.config.ts without custom configuration .
The defineConfig helper from Jest provides type inference for the configuration object:
const { defineConfig } = require("jest");
module.exports = defineConfig({
testEnvironment: "jsdom",
});
The configuration file is discovered automatically when it is at the project root and named jest.config.js (or one of the supported extensions). A project that uses a different file name passes it with --config path/to/config.js.
Complete Example Session
# ============================================
# PART 1: INSTALL
# ============================================
npm install --save-dev jest
# ============================================
# PART 2: THE PACKAGE SCRIPT
# ============================================
# package.json
# {
# "scripts": {
# "test": "jest"
# }
# }
# ============================================
# PART 3: THE FIRST TEST (NO CONFIG NEEDED)
# ============================================
# sum.js
# function sum(a, b) { return a + b; }
# module.exports = { sum };
#
# sum.test.js
# const { sum } = require("./sum");
# test("adds 1 + 2 to equal 3", () => {
# expect(sum(1, 2)).toBe(3);
# });
# ============================================
# PART 4: RUN
# ============================================
npx jest
# or
npm test
# ============================================
# PART 5: THE CONFIG (IF NEEDED)
# ============================================
# Jest 30: jest --init is REMOVED
# Use the package-manager init:
npm init jest@latest
# Or write jest.config.js by hand:
# module.exports = { testEnvironment: "jsdom" };
The parts covered the install, the package script, the zero-config test, the run command, and the jest --init replacement.
Quick Reference
Install
| Item | Value |
|---|---|
| Command | npm install --save-dev jest |
| Node requirement | 18+ (Jest 30 dropped 14, 16, 19, 21) |
| TypeScript requirement | 5.4+ for Jest types |
| Config file | Optional |
Config Generation (Jest 30)
| Old | New |
|---|---|
jest --init (removed) | npm init jest@latest |
| โ | yarn create jest |
| โ | pnpm create jest |
Configuration Files
| File | Supported |
|---|---|
jest.config.js | Yes |
jest.config.ts | Yes |
jest.config.mjs | Yes |
jest.config.cjs | Yes |
jest.config.mts | Yes (Jest 30) |
jest.config.cts | Yes (Jest 30) |
Best Practices
โ Do This:
# Install as dev dependency
npm install --save-dev jest # โ
# Use the replacement for jest --init
npm init jest@latest # โ
# Write the config by hand if the scaffolder is not needed
module.exports = { testEnvironment: "jsdom" }; # โ
โ Don’t Do This:
# Run the removed command
jest --init # unknown command in v30 # โ
# Install as production dependency
npm install jest # โ
# Add a config file when the defaults work
# (unnecessary for a conventional project) # โ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
jest --init fails | Removed in Jest 30 | npm init jest@latest |
| Config file not discovered | Wrong name or location | Use jest.config.js at root |
| Node version error | Older than 18 | Upgrade Node |
| TypeScript config not loading | .ts config without support | Use .js or check Jest 30 support |
Real-World Examples
1. Install
npm install --save-dev jest
2. Package script
"test": "jest"
3. First test (no config)
test("adds", () => { expect(1 + 1).toBe(2); });
4. Config generation
npm init jest@latest
5. Manual config
module.exports = { testEnvironment: "jsdom" };
6. Run
npx jest
7. Watch
npx jest --watch
8. Coverage
npx jest --coverage
9. Custom config path
npx jest --config ./config/jest.config.js
10. Check version
npx jest --version
Visual
Install and Config Flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ npm install --save-dev jest โ
โ โ โ
โ โผ โ
โ package.json: "test": "jest" โ
โ โ โ
โ โโโโโโดโโโโโ โ
โ โผ โผ โ
โ defaults need config? โ
โ work? โ โ
โ โ โโโโโโดโโโโโ โ
โ โผ โผ โผ โ
โ run YES NO โ
โ test โ โ โ
โ โผ โผ โ
โ npm init skip โ
โ jest@latest config โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The jest --init Removal
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ JEST 29 JEST 30 โ
โ โ
โ jest --init npm init jest@latest โ
โ (interactive) (package-manager init) โ
โ โ
โ Command removed; the scaffolder still exists, โ
โ invoked through the package manager. โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Zero-Config vs Config
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ZERO-CONFIG CONFIG NEEDED โ
โ โ
โ *.test.js Custom testMatch โ
โ Default environment jsdom for DOM tests โ
โ No transformer TypeScript/JSX โ
โ โ
โ No jest.config.js jest.config.js โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| Install | npm install --save-dev jest |
| Node requirement | 18+ |
| Config generation | npm init jest@latest (replaces jest --init) |
| Removed command | jest --init in Jest 30 |
| Config file | Optional; jest.config.js or .ts |
| First test | No config needed for conventional projects |
Key takeaways:
- The install is one command and the config is optional.
npm install --save-dev jestinstalls the framework, and a project that follows the conventions runs tests without a configuration file . jest --initwas removed in Jest 30. The interactive scaffolder is gone, and the replacement isnpm init jest@latest,yarn create jest, orpnpm create jest.- Jest 30 requires Node 18+. The release dropped Node 14, 16, 19, and 21, and TypeScript projects that use Jest’s types need TypeScript 5.4+ .
- The config file is for projects that deviate from the conventions. A custom test environment, a transformer, or a non-standard file location is what makes
jest.config.jsnecessary. - The removal is part of a broader v30 cleanup. The deprecated matcher aliases, the
--testPathPatternrename, and the jsdom spec-compliance change are the other migration items that accompany the--initremoval .
Remember: The installation is npm install --save-dev jest, and the first test runs without a config. When a config is needed, the command is npm init jest@latest, not jest --init. The old command is gone, and a script that still calls it will fail. The zero-config promise holds for conventional projects; the config file is for the cases where the project does not match the conventions.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!