Code Reference

Install

Jest · Reference cheat sheet

Install

Jest · Reference cheat sheet


📋 Overview

Jest is a zero/low-config JavaScript test runner with assertions, mocks, snapshots, and coverage. Install as a dev dependency; use jest or npm test via scripts.

🔧 Core concepts

PackageRole
jestRunner + expect + mocks
babel-jestTransform modern JS
ts-jest / @swc/jestTypeScript
jest-environment-jsdomDOM for React

Jest 29+ is common; check your major for config keys.

💡 Examples

Install:

npm i -D jest @types/jest
# TypeScript:
npm i -D ts-jest typescript
# React DOM env:
npm i -D jest-environment-jsdom

package.json:

{
  "scripts": {
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage"
  }
}

First test:

// sum.test.js
const sum = (a, b) => a + b;

test('adds', () => {
  expect(sum(1, 2)).toBe(3);
});

Init config:

npx jest --init

Run:

npx jest
npx jest sum.test.js
npx jest -t "adds"

⚠️ Pitfalls

  • Running an old global jest instead of the project binary.
  • Missing transform for ESM/TS — tests fail to parse.
  • Confusing Vitest APIs when both exist in a monorepo.
  • Not setting testEnvironment for DOM code (node vs jsdom).
  • Committing huge coverage folders — gitignore them.

On this page