Skip to content

Commit

Permalink
fix(jest-config): replace jsonlint with parse-json (#12316)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 7, 2022
1 parent 3a7e1c9 commit 932f78e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 749 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,12 +2,14 @@

### Features

- `[jest-config]` Support comments in JSON config file ([#12316](https://github.com/facebook/jest/pull/12316))
- `[pretty-format]` Expose `ConvertAnsi` plugin ([#12308](https://github.com/facebook/jest/pull/12308))

### Fixes

- `[expect]` Add type definitions for asymmetric `closeTo` matcher ([#12304](https://github.com/facebook/jest/pull/12304))
- `[jest-cli]` Load binary via exported API ([#12315](https://github.com/facebook/jest/pull/12315))
- `[jest-config]` Replace `jsonlint` with `parse-json` ([#12316](https://github.com/facebook/jest/pull/12316))
- `[jest-repl]` Make module importable ([#12311](https://github.com/facebook/jest/pull/12311) & [#12315](https://github.com/facebook/jest/pull/12315))

### Chore & Maintenance
Expand Down
1 change: 1 addition & 0 deletions e2e/multiple-configs/jest.config.json
@@ -1,3 +1,4 @@
{
// this is a comment in a JSON file
"displayName": "Config from json file"
}
4 changes: 3 additions & 1 deletion packages/jest-config/package.json
Expand Up @@ -45,8 +45,10 @@
"jest-util": "^27.5.0",
"jest-validate": "^27.5.0",
"micromatch": "^4.0.4",
"parse-json": "^5.2.0",
"pretty-format": "^27.5.0",
"slash": "^3.0.0"
"slash": "^3.0.0",
"strip-json-comments": "^3.1.1"
},
"devDependencies": {
"@types/glob": "^7.1.1",
Expand Down
18 changes: 8 additions & 10 deletions packages/jest-config/src/readConfigFileAndSetRootDir.ts
Expand Up @@ -7,6 +7,8 @@

import * as path from 'path';
import * as fs from 'graceful-fs';
import parseJson = require('parse-json');
import stripJsonComments = require('strip-json-comments');
import type {Service} from 'ts-node';
import type {Config} from '@jest/types';
import {interopRequireDefault, requireOrImportModule} from 'jest-util';
Expand All @@ -15,8 +17,6 @@ import {
JEST_CONFIG_EXT_TS,
PACKAGE_JSON,
} from './constants';
// @ts-expect-error: vendored
import jsonlint from './vendor/jsonlint';

// Read the configuration and set its `rootDir`
// 1. If it's a `package.json` file, we look into its "jest" property
Expand All @@ -33,23 +33,21 @@ export default async function readConfigFileAndSetRootDir(
try {
if (isTS) {
configObject = await loadTSConfigFile(configPath);
} else if (isJSON) {
const fileContent = fs.readFileSync(configPath, 'utf8');
configObject = parseJson(stripJsonComments(fileContent), configPath);
} else {
configObject = await requireOrImportModule<any>(configPath);
}
} catch (error: unknown) {
if (isJSON) {
throw new Error(
`Jest: Failed to parse config file ${configPath}\n` +
` ${jsonlint.errors(fs.readFileSync(configPath, 'utf8'))}`,
);
} else if (isTS) {
if (isTS) {
throw new Error(
`Jest: Failed to parse the TypeScript config file ${configPath}\n` +
` ${error}`,
);
} else {
throw error;
}

throw error;
}

if (configPath.endsWith(PACKAGE_JSON)) {
Expand Down

0 comments on commit 932f78e

Please sign in to comment.