Skip to content

Jest Setup

This lesson walks through installing Jest into a Node.js project, wiring up npm scripts, and configuring Jest for common setups like Babel and TypeScript so import/export syntax works correctly.

Installing Jest

Jest is distributed as an npm package. In any Node.js project, install it as a dev dependency, since tests only run during development and CI, never in production.

For plain CommonJS Node.js projects (using require/module.exports), no extra configuration is required. For projects using ES module import/export syntax or JSX, you typically add Babel so Jest can transform the code before running it.

npm install --save-dev jest

// package.json
{
  "scripts": {
    "test": "jest"
  }
}

After installing, npm test runs the full Jest suite using default configuration.

Babel Setup for import/export Syntax

npm install --save-dev babel-jest @babel/core @babel/preset-env

// babel.config.js
module.exports = {
  presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
};
  • babel-jest lets Jest transform files through Babel before executing tests.
  • @babel/preset-env compiles modern syntax like import/export to CommonJS for Node.js.
  • For React, also add @babel/preset-react; for TypeScript, add @babel/preset-typescript or use ts-jest.
  • No jest.config.js is required for a minimal setup — Babel config alone is often enough.

Jest Setup Cheat Sheet

Common installation commands for different project types.

Project Type Install Command
Plain Node.js (CommonJS) npm install --save-dev jest
ESM / import syntax npm install --save-dev jest babel-jest @babel/preset-env
React npm install --save-dev jest @testing-library/react @testing-library/jest-dom
TypeScript npm install --save-dev jest ts-jest @types/jest
Init config wizard npx jest --init
Run once npx jest
Run with coverage npx jest --coverage

Setting Up Jest with TypeScript

For TypeScript projects, ts-jest is the most common transformer. It type-checks and compiles .ts files on the fly so Jest can run them directly, and it integrates with your existing tsconfig.json.

npm install --save-dev jest ts-jest @types/jest typescript
npx ts-jest config:init

// jest.config.js (generated)
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

npx ts-jest config:init scaffolds a working jest.config.js automatically.

Useful npm Scripts

A small set of consistent npm scripts saves everyone on a team from memorizing raw Jest flags.

// package.json
{
  "scripts": {
    "test": "jest",
    "test:watch": "jest --watch",
    "test:coverage": "jest --coverage"
  }
}

Common Mistakes

  • Installing Jest as a regular dependency instead of a dev dependency.
  • Forgetting Babel configuration and getting "Unexpected token 'export'" errors.
  • Mixing ts-jest and Babel-based TypeScript transforms in the same project.
  • Not committing jest.config.js or babel.config.js, breaking CI for teammates.

Key Takeaways

  • Install Jest with npm install --save-dev jest.
  • Plain CommonJS projects need no extra configuration.
  • ESM, JSX, and TypeScript projects typically need Babel or ts-jest.
  • npx jest --init scaffolds a starter configuration file interactively.

Pro Tip

Run npx jest --init even in a project you plan to configure by hand. Reading the generated comments in the config file is one of the fastest ways to learn what options exist.