From c262142c2f2f5403c20a5fe7b385742c45ce58a1 Mon Sep 17 00:00:00 2001 From: Ryan Sullivan Date: Sun, 26 Feb 2017 07:00:29 -0800 Subject: [PATCH 1/6] Override Jest config collectCoverageFrom with package.json --- packages/react-scripts/scripts/utils/createJestConfig.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/react-scripts/scripts/utils/createJestConfig.js b/packages/react-scripts/scripts/utils/createJestConfig.js index 5ba7fd16cb2..a9ed34a30b7 100644 --- a/packages/react-scripts/scripts/utils/createJestConfig.js +++ b/packages/react-scripts/scripts/utils/createJestConfig.js @@ -45,5 +45,9 @@ module.exports = (resolve, rootDir, isEjecting) => { if (rootDir) { config.rootDir = rootDir; } + const packageConfig = require(paths.appPackageJson).jest; + if (packageConfig) { + config.collectCoverageFrom = packageConfig.collectCoverageFrom || config.collectCoverageFrom; + } return config; }; From 9f7ec965d8e6a49d36a0f2e68554f6fd78fdbbdc Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 16 May 2017 04:46:44 +0100 Subject: [PATCH 2/6] Protect against overriding other options --- .../scripts/utils/createJestConfig.js | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/react-scripts/scripts/utils/createJestConfig.js b/packages/react-scripts/scripts/utils/createJestConfig.js index 2ee310c681f..8c7b311d075 100644 --- a/packages/react-scripts/scripts/utils/createJestConfig.js +++ b/packages/react-scripts/scripts/utils/createJestConfig.js @@ -10,6 +10,7 @@ 'use strict'; const fs = require('fs'); +const chalk = require('chalk'); const paths = require('../../config/paths'); module.exports = (resolve, rootDir, isEjecting) => { @@ -27,7 +28,7 @@ module.exports = (resolve, rootDir, isEjecting) => { setupTestFrameworkScriptFile: setupTestsFile, testMatch: [ '/src/**/__tests__/**/*.js?(x)', - '/src/**/?(*.)(spec|test).js?(x)' + '/src/**/?(*.)(spec|test).js?(x)', ], testEnvironment: 'node', testURL: 'http://localhost', @@ -46,9 +47,44 @@ module.exports = (resolve, rootDir, isEjecting) => { if (rootDir) { config.rootDir = rootDir; } - const packageConfig = require(paths.appPackageJson).jest; - if (packageConfig) { - config.collectCoverageFrom = packageConfig.collectCoverageFrom || config.collectCoverageFrom; + const overrides = Object.assign({}, require(paths.appPackageJson).jest); + if (overrides) { + if (overrides.collectCoverageFrom) { + config.collectCoverageFrom = overrides.collectCoverageFrom; + delete overrides.collectCoverageFrom; + } + if (overrides.coverageReporters) { + config.coverageReporters = overrides.coverageReporters; + delete overrides.coverageReporters; + } + if (overrides.coverageThreshold) { + config.coverageThreshold = overrides.coverageThreshold; + delete overrides.coverageThreshold; + } + if (overrides.snapshotSerializers) { + config.snapshotSerializers = overrides.snapshotSerializers; + delete overrides.snapshotSerializers; + } + const unsupportedKeys = Object.keys(overrides); + if (unsupportedKeys.length) { + console.error( + chalk.red( + 'By default, Create React App only supports overriding the following Jest options: ' + + chalk.bold('collectCoverageFrom') + + ', ' + + chalk.bold('coverageReporters') + + ', ' + + chalk.bold('coverageThreshold') + + ', and ' + + chalk.bold('snapshotSerializers') + + '\n\nIf you wish to override other 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.\n' + ) + ); + process.exit(1); + } } return config; }; From 121e065b91bc18bca44a2ae78d56a17645ca608f Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 16 May 2017 04:56:12 +0100 Subject: [PATCH 3/6] Better error message --- packages/react-scripts/scripts/utils/createJestConfig.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/react-scripts/scripts/utils/createJestConfig.js b/packages/react-scripts/scripts/utils/createJestConfig.js index 8c7b311d075..ae11795dde2 100644 --- a/packages/react-scripts/scripts/utils/createJestConfig.js +++ b/packages/react-scripts/scripts/utils/createJestConfig.js @@ -77,6 +77,11 @@ module.exports = (resolve, rootDir, isEjecting) => { chalk.bold('coverageThreshold') + ', and ' + chalk.bold('snapshotSerializers') + + '.' + + '\n\nThe following 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 options, you need to eject from the default setup. ' + 'You can do so by running ' + chalk.bold('npm run eject') + From 955c6b89055517365f0ae7caefaa8c87a3e996d4 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 16 May 2017 05:01:25 +0100 Subject: [PATCH 4/6] Create Jest config early on eject --- packages/react-scripts/scripts/eject.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/react-scripts/scripts/eject.js b/packages/react-scripts/scripts/eject.js index 28a21b9ed01..c7861e16c35 100644 --- a/packages/react-scripts/scripts/eject.js +++ b/packages/react-scripts/scripts/eject.js @@ -78,6 +78,13 @@ inquirer folders.forEach(verifyAbsent); files.forEach(verifyAbsent); + // Prepare Jest config early in case it throws + const jestConfig = createJestConfig( + filePath => path.posix.join('', filePath), + null, + true + ); + console.log(); console.log(cyan(`Copying files into ${appPath}`)); @@ -151,11 +158,7 @@ inquirer console.log(cyan('Configuring package.json')); // Add Jest config console.log(` Adding ${cyan('Jest')} configuration`); - appPackage.jest = createJestConfig( - filePath => path.posix.join('', filePath), - null, - true - ); + appPackage.jest = jestConfig; // Add Babel config console.log(` Adding ${cyan('Babel')} preset`); From 29e8f8634fe3640be71731e927ab47754f0fb394 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 16 May 2017 05:06:55 +0100 Subject: [PATCH 5/6] Tweak wording --- .../react-scripts/scripts/utils/createJestConfig.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/react-scripts/scripts/utils/createJestConfig.js b/packages/react-scripts/scripts/utils/createJestConfig.js index ae11795dde2..80b28182a13 100644 --- a/packages/react-scripts/scripts/utils/createJestConfig.js +++ b/packages/react-scripts/scripts/utils/createJestConfig.js @@ -77,15 +77,17 @@ module.exports = (resolve, rootDir, isEjecting) => { chalk.bold('coverageThreshold') + ', and ' + chalk.bold('snapshotSerializers') + - '.' + - '\n\nThe following options in your package.json Jest configuration are not currently supported by Create React App:\n\n' + + '.\n\n' + + 'The following 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 options, you need to eject from the default setup. ' + - 'You can do so by running ' + + '\n\nIf you wish to override other 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.\n' + ' but remember that this is a one-way operation. ' + + 'You may also file an issue with Create React App to discuss adding more options.\n' ) ); process.exit(1); From 11b8c67aa1df48484dc270e86a2280f517a7c49b Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 16 May 2017 05:13:00 +0100 Subject: [PATCH 6/6] Dry it up --- .../scripts/utils/createJestConfig.js | 46 ++++++++----------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/packages/react-scripts/scripts/utils/createJestConfig.js b/packages/react-scripts/scripts/utils/createJestConfig.js index 80b28182a13..13ae8b30411 100644 --- a/packages/react-scripts/scripts/utils/createJestConfig.js +++ b/packages/react-scripts/scripts/utils/createJestConfig.js @@ -48,46 +48,38 @@ module.exports = (resolve, rootDir, isEjecting) => { config.rootDir = rootDir; } const overrides = Object.assign({}, require(paths.appPackageJson).jest); + const supportedKeys = [ + 'collectCoverageFrom', + 'coverageReporters', + 'coverageThreshold', + 'snapshotSerializers', + ]; if (overrides) { - if (overrides.collectCoverageFrom) { - config.collectCoverageFrom = overrides.collectCoverageFrom; - delete overrides.collectCoverageFrom; - } - if (overrides.coverageReporters) { - config.coverageReporters = overrides.coverageReporters; - delete overrides.coverageReporters; - } - if (overrides.coverageThreshold) { - config.coverageThreshold = overrides.coverageThreshold; - delete overrides.coverageThreshold; - } - if (overrides.snapshotSerializers) { - config.snapshotSerializers = overrides.snapshotSerializers; - delete overrides.snapshotSerializers; - } + 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( - 'By default, Create React App only supports overriding the following Jest options: ' + - chalk.bold('collectCoverageFrom') + - ', ' + - chalk.bold('coverageReporters') + - ', ' + - chalk.bold('coverageThreshold') + - ', and ' + - chalk.bold('snapshotSerializers') + + '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' + - 'The following options in your package.json Jest configuration ' + + '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 options, you need to ' + + '\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 adding more options.\n' + 'You may also file an issue with Create React App to discuss ' + + 'supporting more options out of the box.\n' ) ); process.exit(1);