jest.config.js Builder

Generate a Jest configuration for unit and integration testing

Generate a jest.config.js with testEnvironment, preset, transform, moduleNameMapper, setupFilesAfterEnv, coverageThreshold, and testMatch options for JavaScript or TypeScript projects using ts-jest, Babel, or SWC.

When should I use jsdom versus node?

Use jsdom when your tests touch the DOM — React components, browser APIs, or anything rendering markup. Use node for backend, API, and pure-logic tests. jsdom is slower, so default to node and switch only the suites that need a browser-like environment.

Generate a Jest config for your test stack

Jest’s defaults work for plain JavaScript, but real projects need a transform for TypeScript, the right environment for DOM tests, path-alias mapping, and coverage gates. This builder collects those choices and emits a clean jest.config.js with the matching preset, transform, moduleNameMapper, and coverageThreshold blocks, plus the npm install command for whatever transformer you select.

How it works

Jest reads a config object that controls how it discovers, transforms, and runs tests. The key fields the builder sets:

  • testEnvironmentnode for backend logic or jsdom for component tests.
  • transform / preset — how source is compiled. ts-jest adds the preset: "ts-jest" line; Babel uses babel-jest; SWC uses @swc/jest.
  • moduleNameMapper — rewrites imports, mirroring your tsconfig aliases:
moduleNameMapper: {
  "^@/(.*)$": "<rootDir>/src/$1"
}
  • coverageThreshold — minimum branch/function/line/statement percentages that jest --coverage enforces, failing CI if coverage drops below them.

The <rootDir> token is Jest’s placeholder for the directory containing the config, so paths stay portable across machines and CI.

Tips and notes

  • Default testEnvironment to node and only switch DOM-heavy suites to jsdom; it keeps the bulk of your tests fast.
  • When you add tsconfig path aliases, mirror them in moduleNameMapper or Jest will fail to resolve the imports your editor accepts.
  • Map CSS and asset imports to a stub module so component tests do not choke on non-JavaScript files.
  • Start coverage thresholds modestly and ratchet them up; an unreachably high bar just gets bypassed.