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
| Package | Role |
|---|---|
jest | Runner + expect + mocks |
babel-jest | Transform modern JS |
ts-jest / @swc/jest | TypeScript |
jest-environment-jsdom | DOM 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-jsdompackage.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 --initRun:
npx jest
npx jest sum.test.js
npx jest -t "adds"⚠️ Pitfalls
- Running an old global
jestinstead of the project binary. - Missing transform for ESM/TS — tests fail to parse.
- Confusing Vitest APIs when both exist in a monorepo.
- Not setting
testEnvironmentfor DOM code (nodevsjsdom). - Committing huge coverage folders — gitignore them.