Skip to content

Commit 10a180b

Browse files
ryansullygaearon
authored andcommitted
Make coverage and snapshot Jest options overridable in package.json (#1830)
* Override Jest config collectCoverageFrom with package.json * Protect against overriding other options * Better error message * Create Jest config early on eject * Tweak wording * Dry it up
1 parent db2f2ed commit 10a180b

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

packages/react-scripts/scripts/eject.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ inquirer
7878
folders.forEach(verifyAbsent);
7979
files.forEach(verifyAbsent);
8080

81+
// Prepare Jest config early in case it throws
82+
const jestConfig = createJestConfig(
83+
filePath => path.posix.join('<rootDir>', filePath),
84+
null,
85+
true
86+
);
87+
8188
console.log();
8289
console.log(cyan(`Copying files into ${appPath}`));
8390

@@ -151,11 +158,7 @@ inquirer
151158
console.log(cyan('Configuring package.json'));
152159
// Add Jest config
153160
console.log(` Adding ${cyan('Jest')} configuration`);
154-
appPackage.jest = createJestConfig(
155-
filePath => path.posix.join('<rootDir>', filePath),
156-
null,
157-
true
158-
);
161+
appPackage.jest = jestConfig;
159162

160163
// Add Babel config
161164
console.log(` Adding ${cyan('Babel')} preset`);

packages/react-scripts/scripts/utils/createJestConfig.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'use strict';
1111

1212
const fs = require('fs');
13+
const chalk = require('chalk');
1314
const paths = require('../../config/paths');
1415

1516
module.exports = (resolve, rootDir, isEjecting) => {
@@ -27,7 +28,7 @@ module.exports = (resolve, rootDir, isEjecting) => {
2728
setupTestFrameworkScriptFile: setupTestsFile,
2829
testMatch: [
2930
'<rootDir>/src/**/__tests__/**/*.js?(x)',
30-
'<rootDir>/src/**/?(*.)(spec|test).js?(x)'
31+
'<rootDir>/src/**/?(*.)(spec|test).js?(x)',
3132
],
3233
testEnvironment: 'node',
3334
testURL: 'http://localhost',
@@ -46,5 +47,43 @@ module.exports = (resolve, rootDir, isEjecting) => {
4647
if (rootDir) {
4748
config.rootDir = rootDir;
4849
}
50+
const overrides = Object.assign({}, require(paths.appPackageJson).jest);
51+
const supportedKeys = [
52+
'collectCoverageFrom',
53+
'coverageReporters',
54+
'coverageThreshold',
55+
'snapshotSerializers',
56+
];
57+
if (overrides) {
58+
supportedKeys.forEach(key => {
59+
if (overrides.hasOwnProperty(key)) {
60+
config[key] = overrides[key];
61+
delete overrides[key];
62+
}
63+
});
64+
const unsupportedKeys = Object.keys(overrides);
65+
if (unsupportedKeys.length) {
66+
console.error(
67+
chalk.red(
68+
'Out of the box, Create React App only supports overriding ' +
69+
'these Jest options:\n\n' +
70+
supportedKeys.map(key => chalk.bold(' \u2022 ' + key)).join('\n') +
71+
'.\n\n' +
72+
'These options in your package.json Jest configuration ' +
73+
'are not currently supported by Create React App:\n\n' +
74+
unsupportedKeys
75+
.map(key => chalk.bold(' \u2022 ' + key))
76+
.join('\n') +
77+
'\n\nIf you wish to override other Jest options, you need to ' +
78+
'eject from the default setup. You can do so by running ' +
79+
chalk.bold('npm run eject') +
80+
' but remember that this is a one-way operation. ' +
81+
'You may also file an issue with Create React App to discuss ' +
82+
'supporting more options out of the box.\n'
83+
)
84+
);
85+
process.exit(1);
86+
}
87+
}
4988
return config;
5089
};

0 commit comments

Comments
 (0)