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:
- testEnvironment —
nodefor backend logic orjsdomfor component tests. - transform / preset — how source is compiled.
ts-jestadds thepreset: "ts-jest"line; Babel usesbabel-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 --coverageenforces, 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
testEnvironmenttonodeand only switch DOM-heavy suites tojsdom; it keeps the bulk of your tests fast. - When you add tsconfig path aliases, mirror them in
moduleNameMapperor 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.