| | |

Jest 02 ๐Ÿƒ Installation โ€” npm install -D jest & the jest –init Removal in v30

โฑ๏ธ Estimated read: 7 min | |

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

ItemValue
Commandnpm install --save-dev jest
Node requirement18+ (Jest 30 dropped 14, 16, 19, 21)
TypeScript requirement5.4+ for Jest types
Config fileOptional

Config Generation (Jest 30)

OldNew
jest --init (removed)npm init jest@latest
โ€”yarn create jest
โ€”pnpm create jest

Configuration Files

FileSupported
jest.config.jsYes
jest.config.tsYes
jest.config.mjsYes
jest.config.cjsYes
jest.config.mtsYes (Jest 30)
jest.config.ctsYes (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

PitfallWhy It HappensFix
jest --init failsRemoved in Jest 30npm init jest@latest
Config file not discoveredWrong name or locationUse jest.config.js at root
Node version errorOlder than 18Upgrade Node
TypeScript config not loading.ts config without supportUse .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

ItemValue
Installnpm install --save-dev jest
Node requirement18+
Config generationnpm init jest@latest (replaces jest --init)
Removed commandjest --init in Jest 30
Config fileOptional; jest.config.js or .ts
First testNo config needed for conventional projects

Key takeaways:

  • The install is one command and the config is optional. npm install --save-dev jest installs the framework, and a project that follows the conventions runs tests without a configuration file .
  • jest --init was removed in Jest 30. The interactive scaffolder is gone, and the replacement is npm init jest@latest, yarn create jest, or pnpm 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.js necessary.
  • The removal is part of a broader v30 cleanup. The deprecated matcher aliases, the --testPathPattern rename, and the jsdom spec-compliance change are the other migration items that accompany the --init removal .

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.

Was this helpful?


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!