Jest config generator — jest.config.js with ts-jest and coverage

Pick the test environment, whether to compile TypeScript with ts-jest and how much coverage you require, and the tool writes a jest.config.js together with the npm command that installs every package it mentions.

Live demo · real result from this tool
Test environment
Module format

jest.config.js

/** @type {import('jest').Config} */const config = {  // Compile TypeScript test files with ts-jest  preset: 'ts-jest',  testEnvironment: 'node',  roots: ['<rootDir>/src'],  collectCoverage: true,  coverageDirectory: 'coverage',  coverageProvider: 'v8',  collectCoverageFrom: ['src/**/*.{ts,tsx}', '!src/**/*.d.ts'],  coverageThreshold: {    global: { branches: 80, functions: 80, lines: 80, statements: 80 },  },  clearMocks: true,}; module.exports = config; 

Install

npm install --save-dev jest ts-jest @types/jest typescript

Written for Jest 30. With "type": "module" in package.json, choose the ES module format (or name the file jest.config.cjs).

Processed on your device — nothing is uploaded.

How to use the Jest config generator

  1. Choose the Test environment: Node or jsdom (browser-like).
  2. Keep TypeScript via ts-jest ticked for .ts tests, or untick it for plain JavaScript.
  3. Tick Collect coverage and set Coverage threshold %; 0 means no minimum.
  4. Optionally fill in Test roots (comma-separated folders) and a Setup file after environment, such as jest.setup.ts.
  5. Tick Stub CSS imports if components import style files, pick the Module format, then copy the config and run the install command.

What it does and when to use it

Jest runs without any config, but real projects soon need one: TypeScript compilation, a browser-like DOM, a setup file for test helpers, or a coverage gate in CI. This tool writes those settings in one go and lists the packages they depend on, which is where hand-written configs usually go wrong.

If you skip ts-jest, the Babel config generator gives Jest a way to read modern syntax and TypeScript. The tsconfig generator sets up the compiler options ts-jest reads, and the GitHub Actions generator can run npm test on every push so the coverage threshold actually blocks bad merges.

How it works

The file opens with a type comment so editors offer autocomplete, then adds only what you chose:

  • preset: 'ts-jest' and the TypeScript packages when ts-jest is on;
  • testEnvironment, plus jest-environment-jsdom for jsdom;
  • roots and setupFilesAfterEnv, with <rootDir>/ added in front of each path;
  • a moduleNameMapper that sends .css, .less, .scss and .sass imports to identity-obj-proxy;
  • coverage with the V8 provider, gathered from src/**, and a global threshold for branches, functions, lines and statements.

clearMocks: true resets mock calls between tests. Option names match the Jest configuration docs; the tool targets Jest 30.

Worked examples

Defaults: Node, ts-jest, 80% coverage, roots src.

  preset: 'ts-jest',
  testEnvironment: 'node',
  roots: ['<rootDir>/src'],
  collectCoverage: true,
  coverageDirectory: 'coverage',
  coverageProvider: 'v8',
  collectCoverageFrom: ['src/**/*.{ts,tsx}', '!src/**/*.d.ts'],

followed by global: { branches: 80, functions: 80, lines: 80, statements: 80 },. Install: npm install --save-dev jest ts-jest @types/jest typescript.

A JavaScript component library. jsdom, no ts-jest, no coverage, CSS stubs on, ES module, setup file ./jest.setup.js, roots cleared. The config gains setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], and ends with export default config;. Install: npm install --save-dev jest jest-environment-jsdom identity-obj-proxy.

Limits and tips

  • Coverage is always collected from src/. Edit collectCoverageFrom if your code lives elsewhere.
  • Path aliases such as @/ need their own moduleNameMapper entry.
  • The setup file is only referenced. Create it yourself, for example to import @testing-library/jest-dom.

Frequently asked questions

When should I choose jsdom instead of Node?
Choose jsdom when tests touch the DOM: React or Vue components, document, window or localStorage. Server code, libraries and CLI tools run faster in the plain Node environment.
Why is jest-environment-jsdom a separate install?
Jest stopped bundling jsdom in version 28. The tool adds the package to the install command whenever you pick jsdom, and the config line carries a comment saying so.
What happens when coverage falls below the threshold?
Jest prints which metric failed and exits with an error, so a CI job fails. Set the threshold to 0 to collect coverage without enforcing a minimum.
My project uses "type" set to "module". Which format do I pick?
Choose ES module, which ends the file with export default config. If you keep CommonJS in such a project, rename the file jest.config.cjs.
Do I need ts-jest for TypeScript tests?
Not necessarily. ts-jest type-checks while it compiles, which is slower but catches type errors. Babel can strip types faster without checking them; in that case untick ts-jest and add a Babel config.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close