# Jest and JSON5: Config, Fixtures, ts-jest | JSON5.dev


Source: https://json5.dev/config/jest/

---

# Jest Config and Test Fixtures with JSON5

Jest does not read `jest.config.json5`, but it does let you use JSON5 for fixtures, imports and the config itself.

We ran Jest 30.5.2 against each file name and each trick on this page. The supported config file is `jest.config.json`, which accepts comments but nothing else from JSON5; a `.json5` file passed with `--config` is refused.

Below are a working loader for a JSON5 config, a transform that makes `require('./data.json5')` work in tests, the one place a commented `tsconfig.json` really breaks, and the Vitest equivalents.

![A test lab where a commented JSON5 config scroll feeds a Jest test runner and every check turns green](https://json5.dev/img/hero/config-jest.png)

**TL;DR:** Jest finds `jest.config.js|ts|mjs|mts|cjs|cts|json` and nothing named `.json5`. `jest.config.json` takes comments but fails on trailing commas and unquoted keys. For a full JSON5 config, write a three-line `jest.config.cjs` that parses it with the `json5` package. For fixtures, add a `transform` or call `JSON5.parse()` yourself.

[Config Files](#config-files) [A JSON5 Config](#json5-config) [Fixtures](#fixtures) [Transform](#transform) [ts-jest and tsconfig](#ts-jest) [Vitest](#vitest) [Pitfalls](#pitfalls) [FAQ](#faq) [Sources](#references)

![Jest logo](https://json5.dev/images/tech/jest.png)

## Which Config Files Jest Reads

The Jest documentation says a configuration file is discovered automatically when it is named `jest.config.js|ts|mjs|mts|cjs|cts|json`. A `"jest"` key in `package.json`, or a `"jest"` key that points at a JSON file, also works, and `--config` accepts a path or a JSON string. The list ends there: there is no `.json5` extension in it.

Config source

Comments

Trailing commas

Unquoted keys

Result in Jest 30.5.2

`jest.config.json`

![Yes](https://json5.dev/images/icons/yes.png)

![No](https://json5.dev/images/icons/no.png)

![No](https://json5.dev/images/icons/no.png)

Loads with comments. A trailing comma stops Jest with a `JSONError`.

`"jest"` key in `package.json`

![No](https://json5.dev/images/icons/no.png)

![No](https://json5.dev/images/icons/no.png)

![No](https://json5.dev/images/icons/no.png)

Strict JSON, because every tool shares `package.json`.

`jest.config.json5` found automatically

Not applicable

Silently ignored. Jest printed no warning and ran with its defaults, so the file's `testMatch`, `transform` and other settings did not apply (`--showConfig` showed the default values).

`--config jest.config.json5`

Not applicable

Refused: Jest prints its usage text and says `--config` needs "a JSON string literal, or a file path with one of these extensions: .js, .ts, .mjs, .mts, .cjs, .cts, .json".

`jest.config.js`, `.cjs`, `.mjs`, `.ts`, ...

It is code, so anything your code can parse

The route to a real JSON5 config, covered next.

### What the JSON loader does

The loader in `jest-config` reads a `.json` config file, removes comments with the `strip-json-comments` package, then parses the result with `parse-json`. That is why comments work and nothing else does. A file with a [trailing comma](https://json5.dev/glossary/#trailing-comma) fails with `Expected double-quoted property name in JSON`, and a file that starts `{ testMatch: [...] }` fails with `Expected property name or '}' in JSON`. Both messages come from running Jest 30.5.2, not from the documentation.

`{ // jest.config.json: comments are fine here "testEnvironment": "node", /* but the last property must not end in a comma */ "verbose": true }`

Some neighbors are more generous than Jest: see [Babel config](https://json5.dev/config/babel/) and [tsconfig.json](https://json5.dev/config/tsconfig/) for tools that parse a looser dialect.

![Settings icon](https://json5.dev/images/icons/settings.png)

## Using a Real JSON5 Config

A JavaScript config file can return any object, so it can load one. Keep the settings in `jest.config.json5`, where comments, single quotes, unquoted keys and trailing commas all work, and let a tiny `jest.config.cjs` do the parsing. Jest finds the `.cjs` file by name, which is the whole trick.

`// jest.config.cjs const JSON5 = require('json5'); const fs = require('node:fs'); const path = require('node:path'); module.exports = JSON5.parse( fs.readFileSync(path.join(__dirname, 'jest.config.json5'), 'utf8') );`

`// jest.config.json5 { // Only look in the test folder. testMatch: ['**/test/*.test.js'], moduleFileExtensions: ['js', 'json5'], transform: { '\\.json5$': 'json5-jest' }, }`

We ran this arrangement on Jest 30.5.2 and Node 24: Jest picked up the parsed object, applied `testMatch` and the transform, and passed. Three details matter.

-   **Keep only one config file.** A `jest.config.cjs` next to a `jest.config.json` stops Jest with "Multiple configurations found ... Implicit config resolution does not allow multiple configuration files." Delete the JSON file when you switch.
-   **Use `__dirname`.** A relative path would resolve against whatever directory Jest was launched from, which breaks in a monorepo. The `rootDir` default is the folder that holds the config file.
-   **The parser is the `json5` package.** It is a dev dependency of the project, version 2.2.3 at the time of writing. Install it with `npm install -D json5`; the [JavaScript guide](https://json5.dev/javascript/) covers its API.

**Is it worth it?** For a short config, `jest.config.json` with comments is simpler and has no extra file. Reach for the JSON5 loader when the config is long and you want trailing commas or when the file doubles as documentation for a team.

![Test icon](https://json5.dev/images/icons/test.png)

## JSON5 Test Fixtures

A fixture is data a test reads: a sample request, an expected response, a config the code under test has to load. JSON5 suits fixtures because a comment can say why the case exists. `require()` of a `.json5` file does not work out of the box, since Jest tries to run it as JavaScript, so there are two honest options.

### Option 1: read the file and parse it

No configuration at all. It needs nothing beyond the `json5` package, and it ran in a CommonJS test on Jest 30.5.2.

`// test/data.json5 { // A fixture, with a note for the next reader. name: 'demo', retries: 3, hex: 0xFF, list: [1, 2, 3,], // trailing comma } // test/read.test.js const JSON5 = require('json5'); const fs = require('node:fs'); const path = require('node:path'); test('reads the fixture', () => { const text = fs.readFileSync(path.join(__dirname, 'data.json5'), 'utf8'); expect(JSON5.parse(text).hex).toBe(255); });`

### Option 2: import it with a transform

A transform lets the test write `require('./data.json5')` and get an object. The next section shows the configuration.

`// test/req.test.js const data = require('./data.json5'); test('fixture via require', () => { expect(data).toEqual({ name: 'demo', retries: 3, hex: 255, list: [1, 2, 3] }); });`

Both tests passed on Jest 30.5.2. Note that `0xFF` arrives as `255` and the trailing comma is gone: the parser has already done its job. If a test asserts on the file text itself, for example to check a formatter, read it as a string and skip the transform. For the rules behind these values, see the [syntax reference](https://json5.dev/spec/syntax/).

![Plugin icon](https://json5.dev/images/icons/plugin.png)

## A Transform for .json5 Imports

Jest runs every file it loads through the transformer whose pattern matches, so one entry in `transform` turns a JSON5 file into a CommonJS module. A transformer is an object with a `process()` method that returns `{ code }`.

### The package that already exists

`json5-jest` on npm (2.0.0, MIT, last published June 2023) is exactly this transformer. Its whole implementation is the `process()` function below, it depends only on `json5`, and its peer dependency is Jest 28 or newer. We installed it beside Jest 30.5.2 and it worked.

`npm install -D json5-jest json5 // package.json (a "jest" key is strict JSON, so quote everything) { "jest": { "transform": { "\\.json5$": "json5-jest" } } }`

### Or keep it in the repository

A package last published in 2023 is a fair thing to avoid depending on. The same transformer is five lines, and it passed our test with the identical result.

``// json5-transform.cjs const JSON5 = require('json5'); module.exports = { process(sourceText) { return { code: `module.exports = ${JSON.stringify(JSON5.parse(sourceText))};` }; }, }; // jest.config.json5 (read by jest.config.cjs) { transform: { '\\.json5$': '<rootDir>/json5-transform.cjs' }, }``

**Setting `transform` replaces the default.** Jest's default is `{"\\.[jt]sx?$": "babel-jest"}`. If you add your own entry and still need Babel for your source files, list it again: `'\\.[jt]sx?$': 'babel-jest'`. The Jest docs call this out for any custom transformer.

### Extensionless imports and aliases

With the transform alone, `require('./data.json5')` works because the extension is spelled out. To write `require('./data')`, add `json5` to `moduleFileExtensions`; the default list is `js, mjs, cjs, jsx, ts, mts, cts, tsx, json, node`, and setting it replaces that list, so repeat any extension you still need. We verified that `moduleFileExtensions: ['js', 'json5']` resolves the extensionless form.

`moduleNameMapper` is a different tool: it rewrites a module name to a path before resolution. It cannot parse JSON5, but it gives fixtures a short alias, and the mapped file still goes through the transform:

`{ moduleNameMapper: { '^@fx/(.*)$': '<rootDir>/test/$1' }, transform: { '\\.json5$': 'json5-jest' }, } // in a test: resolves to test/data.json5 and returns the parsed object const data = require('@fx/data.json5');`

![TypeScript logo](https://json5.dev/images/tech/typescript.png)

## ts-jest and a tsconfig with Comments

A common worry is that `ts-jest` chokes on a `tsconfig.json` full of comments. It does not, and we tested it. The real failure is somewhere else.

### What works

`ts-jest` 29.4.15 reads the file through TypeScript's own `readConfigFile` and `parseJsonConfigFileContent`, which understand comments and trailing commas, so the config below ran a TypeScript test under TypeScript 6.0.3 with no changes. `tsc` accepts the same file. Details are in the [tsconfig guide](https://json5.dev/config/tsconfig/).

`// tsconfig.json { // compiler settings "compilerOptions": { /* strict mode */ "strict": true, "target": "es2022", "module": "commonjs", "esModuleInterop": true, // needed for default imports "paths": { "@lib/*": ["./src/*"] }, "types": ["jest"], }, }`

### What breaks: requiring the tsconfig yourself

The ts-jest documentation shows `pathsToModuleNameMapper` fed from `compilerOptions.paths`, with the config imported by a line like `import { compilerOptions } from './tsconfig'`. That import goes through Node's JSON loader, which is strict. Once the file has a comment, `jest.config.cjs` throws before any test runs. The wording below is from Node 24 (a file that opens with a comment gets a different message, `Unexpected token '/'`), and Node prints the full path:

`SyntaxError: /path/to/project/tsconfig.json: Expected property name or '}' in JSON at position 4 (line 2 column 3)`

The fix is to parse the file with a parser that accepts comments. Using `json5` works, because comments and trailing commas are part of JSON5:

`// jest.config.cjs const { pathsToModuleNameMapper } = require('ts-jest'); const JSON5 = require('json5'); const fs = require('node:fs'); const { compilerOptions } = JSON5.parse( fs.readFileSync(__dirname + '/tsconfig.json', 'utf8') ); module.exports = { preset: 'ts-jest', testEnvironment: 'node', moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }), };`

With the `paths` entry above in the commented tsconfig, a test that imports `@lib/sum` passed, and `tsc -p .` accepted the file. The same file in a plain `require('./tsconfig.json')` did not load at all. Write the path as `./src/*`: without a `baseUrl`, TypeScript 6.0.3 rejects a non-relative target with `TS5090`, and `baseUrl` itself now draws `TS5101`, a deprecation error that says it stops working in TypeScript 7.0.

### A tsconfig named .json5 is only half a fix

TypeScript will read any file name you hand it, and its parser tolerates comments and trailing commas, so `tsc -p tsconfig.json5` and the ts-jest `tsconfig` option both accept such a file. They stop at the next JSON5 feature: a file with unquoted keys or single quotes failed in both with `error TS1327: String literal with double quotes expected`. Keep the quotes and the `.json` name in a tsconfig; TypeScript's dialect is JSON with comments, not JSON5.

![Vite logo](https://json5.dev/images/tech/vite.png)

## The Vitest Equivalents

Vitest sits on Vite, so the answers come from Vite. It looks for `vitest.config.{ts,mts,cts,js,mjs,cjs}` and then `vite.config.*`; its documentation says it supports every conventional JavaScript and TypeScript extension but not `json`. So there is no JSON config to put comments in, and the config is always code, where comments are native.

Need

Jest 30.5.2

Vitest 5.1.2

Config with comments

`jest.config.json` (comments only), or any code config

A code config; no JSON option

Config written in JSON5

Parse it from a `jest.config.cjs`

Parse it from `vitest.config.js` the same way

`import data from './x.json5'`

A `transform` entry

A Vite plugin, such as `vite-plugin-json5`

Fixture as text

`fs.readFileSync` then `JSON5.parse`

`import text from './x.json5?raw'` then `JSON5.parse`

Short fixture alias

`moduleNameMapper`

`resolve.alias`

Without help, importing a JSON5 file in Vitest fails at parse time with a `RolldownError: Parse failure` pointing into the fixture's first property lines. With `vite-plugin-json5` 1.5.0 in `plugins`, both a default import and a named import from the same file passed. The plugin's options are covered in the [Vite guide](https://json5.dev/config/vite/).

`// vitest.config.js import { defineConfig } from 'vitest/config'; import json5 from 'vite-plugin-json5'; export default defineConfig({ plugins: [json5()], test: { include: ['test/*.test.js'] }, }); // test/data.test.js import { test, expect } from 'vitest'; import data, { name } from './data.json5'; test('fixture import', () => { expect(data.hex).toBe(255); expect(name).toBe('demo'); });`

If you would rather add no plugin, the `?raw` suffix is built into Vite: `import text from './data.json5?raw'` gives the file as a string, and `JSON5.parse(text)` finishes the job. We ran that form in Vitest 5.1.2 with Vite 8.4.0 and it passed.

![Warning icon](https://json5.dev/images/icons/warning.png)

## Pitfalls

### A silently ignored config

A `jest.config.json5` next to your tests is never loaded and never reported. Jest runs with defaults, so a `testMatch` or a coverage threshold in that file simply does not apply. Keep a loader in place, and check with `npx jest --showConfig`.

### Two config files

Leaving the old `jest.config.json` beside a new `jest.config.cjs` stops Jest outright with "Multiple configurations found". Delete the one you replaced.

### Transform without Babel

Your `transform` map replaces the default `babel-jest` entry. If your sources use syntax Node cannot run, add `'\\.[jt]sx?$': 'babel-jest'` back or the suite fails on the first `import`.

### Values the transform flattens

The transformer emits `JSON.stringify` output, so `Infinity` and `NaN` in a fixture become `null`, and comments are gone. Assert on the raw text when the exact characters matter.

### Stale caches after changing a transformer

Jest caches transformed files. When you edit your own transformer or swap parsers and results look wrong, run `npx jest --no-cache` once before debugging further.

![Question icon](https://json5.dev/images/icons/question.png)

## Common Questions

### Does Jest support jest.config.json5?

No. In Jest 30.5.2 the file is ignored when found automatically and rejected by `--config`. Use `jest.config.json` for comments, or a `jest.config.cjs` that parses a `.json5` file.

### Can jest.config.json contain comments?

Yes, both `//` and `/* */`. It cannot contain trailing commas, single quotes or unquoted keys, and the failure is an immediate `JSONError`.

### How do I import a JSON5 file in a Jest test?

Add a `transform` for `\.json5$`, either the `json5-jest` package or a five-line transformer of your own, or skip configuration and call `JSON5.parse()` on the file text.

### Does ts-jest fail on a tsconfig with comments?

No. It reads the file with TypeScript's own parser. What fails is a `require('./tsconfig.json')` in your Jest config, for example to build `moduleNameMapper` from `paths`. Parse the file with `JSON5.parse` there.

### What about Vitest?

It has no JSON config file at all, so config is code. For fixtures, add `vite-plugin-json5` or import the file with `?raw` and parse it.

### Should I store fixtures as JSON or JSON5?

Use JSON5 when a fixture needs a comment that explains the case, or many hand-edited values. Use plain JSON for anything generated or shared with tools that expect it. The [migration guide](https://json5.dev/tutorials/migration-from-json/) covers the conversion.

## Related Pages

[![TypeScript logo](https://json5.dev/images/tech/typescript.png)

### tsconfig.json

The JSON-with-comments dialect ts-jest reads

](https://json5.dev/config/tsconfig/)[![Vite logo](https://json5.dev/images/tech/vite.png)

### Vite

Plugin options for importing JSON5 in Vitest

](https://json5.dev/config/vite/)[![Babel logo](https://json5.dev/images/tech/babel.png)

### Babel

The transformer Jest applies by default

](https://json5.dev/config/babel/)

## Sources

(9)

1.  [Jest documentation: Configuring Jest](https://jestjs.io/docs/configuration) (supported file names, `transform`, `moduleFileExtensions`, `moduleNameMapper`)
2.  [Jest configuration source on GitHub](https://github.com/jestjs/jest/blob/main/docs/Configuration.md), read for the 30.x documentation text
3.  [npm: jest 30.5.2](https://www.npmjs.com/package/jest), whose `jest-config` loader was read to confirm the comment-stripping and JSON parsing steps
4.  [npm: json5-jest 2.0.0](https://www.npmjs.com/package/json5-jest) (MIT, Jest 28 or newer)
5.  [ts-jest documentation: Paths mapping](https://kulshekhar.github.io/ts-jest/docs/getting-started/paths-mapping)
6.  [npm: ts-jest 29.4.15](https://www.npmjs.com/package/ts-jest), source read for its `readConfigFile` use
7.  [Vitest documentation: Configuring Vitest](https://vitest.dev/config/) (config file names, no `json`)
8.  [npm: vite-plugin-json5 1.5.0](https://www.npmjs.com/package/vite-plugin-json5)
9.  [The JSON5 specification](https://json5.org/)

Last reviewed: November 29, 2026.
