Commit 10a180be authored by Ryan Sullivan's avatar Ryan Sullivan Committed by Dan Abramov
Browse files

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
parent db2f2ed9
3 merge requests!12191Lim.Pisey.168:/Identified - We are currently investigating reports of missing build logs. The issue has been identified and a resolution is in progress. We will provide a further update when available.Mar 21, 09:02 UTC,!12853brikk,!5717Automatically extract project file structure from build bundle file
Showing with 48 additions and 6 deletions
+48 -6
...@@ -78,6 +78,13 @@ inquirer ...@@ -78,6 +78,13 @@ inquirer
folders.forEach(verifyAbsent); folders.forEach(verifyAbsent);
files.forEach(verifyAbsent); files.forEach(verifyAbsent);
// Prepare Jest config early in case it throws
const jestConfig = createJestConfig(
filePath => path.posix.join('<rootDir>', filePath),
null,
true
);
console.log(); console.log();
console.log(cyan(`Copying files into ${appPath}`)); console.log(cyan(`Copying files into ${appPath}`));
...@@ -151,11 +158,7 @@ inquirer ...@@ -151,11 +158,7 @@ inquirer
console.log(cyan('Configuring package.json')); console.log(cyan('Configuring package.json'));
// Add Jest config // Add Jest config
console.log(` Adding ${cyan('Jest')} configuration`); console.log(` Adding ${cyan('Jest')} configuration`);
appPackage.jest = createJestConfig( appPackage.jest = jestConfig;
filePath => path.posix.join('<rootDir>', filePath),
null,
true
);
// Add Babel config // Add Babel config
console.log(` Adding ${cyan('Babel')} preset`); console.log(` Adding ${cyan('Babel')} preset`);
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
'use strict'; 'use strict';
const fs = require('fs'); const fs = require('fs');
const chalk = require('chalk');
const paths = require('../../config/paths'); const paths = require('../../config/paths');
module.exports = (resolve, rootDir, isEjecting) => { module.exports = (resolve, rootDir, isEjecting) => {
...@@ -27,7 +28,7 @@ module.exports = (resolve, rootDir, isEjecting) => { ...@@ -27,7 +28,7 @@ module.exports = (resolve, rootDir, isEjecting) => {
setupTestFrameworkScriptFile: setupTestsFile, setupTestFrameworkScriptFile: setupTestsFile,
testMatch: [ testMatch: [
'<rootDir>/src/**/__tests__/**/*.js?(x)', '<rootDir>/src/**/__tests__/**/*.js?(x)',
'<rootDir>/src/**/?(*.)(spec|test).js?(x)' '<rootDir>/src/**/?(*.)(spec|test).js?(x)',
], ],
testEnvironment: 'node', testEnvironment: 'node',
testURL: 'http://localhost', testURL: 'http://localhost',
...@@ -46,5 +47,43 @@ module.exports = (resolve, rootDir, isEjecting) => { ...@@ -46,5 +47,43 @@ module.exports = (resolve, rootDir, isEjecting) => {
if (rootDir) { if (rootDir) {
config.rootDir = rootDir; config.rootDir = rootDir;
} }
const overrides = Object.assign({}, require(paths.appPackageJson).jest);
const supportedKeys = [
'collectCoverageFrom',
'coverageReporters',
'coverageThreshold',
'snapshotSerializers',
];
if (overrides) {
supportedKeys.forEach(key => {
if (overrides.hasOwnProperty(key)) {
config[key] = overrides[key];
delete overrides[key];
}
});
const unsupportedKeys = Object.keys(overrides);
if (unsupportedKeys.length) {
console.error(
chalk.red(
'Out of the box, Create React App only supports overriding ' +
'these Jest options:\n\n' +
supportedKeys.map(key => chalk.bold(' \u2022 ' + key)).join('\n') +
'.\n\n' +
'These options in your package.json Jest configuration ' +
'are not currently supported by Create React App:\n\n' +
unsupportedKeys
.map(key => chalk.bold(' \u2022 ' + key))
.join('\n') +
'\n\nIf you wish to override other Jest options, you need to ' +
'eject from the default setup. You can do so by running ' +
chalk.bold('npm run eject') +
' but remember that this is a one-way operation. ' +
'You may also file an issue with Create React App to discuss ' +
'supporting more options out of the box.\n'
)
);
process.exit(1);
}
}
return config; return config;
}; };
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment