Skip to content

jest.config.js

While Jest works with almost no configuration, real projects usually need a jest.config.js file for a handful of common options. This lesson tours the settings you'll actually use.

Creating a Configuration File

A Jest configuration file exports a plain object (or a function returning one) describing how Jest should behave: which environment to use, how to resolve modules, what to include in coverage, and more. You can also put configuration under a "jest" key in package.json instead of a separate file.

Running npx jest --init launches an interactive prompt that generates a well-commented starter jest.config.js based on your answers, which is a great way to discover available options.

// jest.config.js
module.exports = {
  testEnvironment: 'node',
  verbose: true,
  collectCoverage: true,
  coverageDirectory: 'coverage',
  testPathIgnorePatterns: ['/node_modules/', '/dist/'],
};

A minimal but realistic configuration: environment, verbose output, and coverage collection.

Frequently Used Options

module.exports = {
  testEnvironment: 'node',
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
  },
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
  },
  collectCoverageFrom: ['src/**/*.js', '!src/**/*.test.js'],
};
  • moduleNameMapper remaps import paths, commonly used to mirror path aliases from tsconfig.json.
  • transform tells Jest which tool compiles which file types before running them.
  • collectCoverageFrom controls which source files are included in coverage reports (not just tested ones).
  • clearMocks: true automatically clears mock call history before every test, without an explicit afterEach().

jest.config.js Cheat Sheet

The configuration options you'll reach for most often.

Option Purpose
testEnvironment 'node' or 'jsdom'
testMatch / testPathIgnorePatterns Control which files are treated as tests
setupFilesAfterEnv Run shared setup after Jest globals are ready
moduleNameMapper Remap import paths and aliases
transform Configure Babel/TypeScript compilation
collectCoverage / coverageThreshold Enable and enforce code coverage
clearMocks / resetMocks Automatically reset mocks between tests
verbose Print each individual test name in the output

Using moduleNameMapper for Path Aliases

If your project uses import aliases (like @/components/Button instead of ../../components/Button) configured in tsconfig.json or a bundler, Jest doesn't understand those aliases automatically — moduleNameMapper teaches it how to resolve them.

// tsconfig.json path alias: "@/*": ["src/*"]
module.exports = {
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
  },
};

Configuring Jest Inside package.json

For small projects, it's common to skip a separate config file entirely and nest Jest's configuration under a "jest" key in package.json. Functionally, this is identical to jest.config.js.

// package.json
{
  "jest": {
    "testEnvironment": "node",
    "collectCoverage": true
  }
}

Common Mistakes

  • Forgetting <rootDir> is a required token for absolute-feeling paths in Jest configuration.
  • Configuring path aliases in tsconfig.json/bundler config but forgetting to mirror them in moduleNameMapper.
  • Maintaining both a jest.config.js and a "jest" key in package.json at the same time (only one is used).
  • Copy-pasting a config from another project without understanding what each option actually does.

Key Takeaways

  • Jest configuration lives in jest.config.js or a "jest" key in package.json.
  • npx jest --init is the fastest way to explore available configuration options interactively.
  • moduleNameMapper and transform are essential for path aliases and non-JS file types.
  • Coverage-related options (collectCoverage, coverageThreshold) are configured here too.

Pro Tip

Keep jest.config.js under version control and treat changes to it like code review-worthy changes — a small tweak to testPathIgnorePatterns or collectCoverageFrom can silently hide entire files from your test run or coverage report.