Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jest-config): replace jsonlint with parse-json #12316

Merged
merged 3 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading