Skip to content

Add overrideConfig option for the react-scripts test script #1659

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

Closed
Closed
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
10 changes: 9 additions & 1 deletion packages/react-scripts/scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require('dotenv').config({silent: true});

const jest = require('jest');
const argv = process.argv.slice(2);
const overrideArg = (process.argv.filter((i) => i.match('--overrideConfig')) || [])[0];

// Watch unless on CI or in coverage mode
if (!process.env.CI && argv.indexOf('--coverage') < 0) {
Expand All @@ -31,10 +32,17 @@ if (!process.env.CI && argv.indexOf('--coverage') < 0) {
const createJestConfig = require('../utils/createJestConfig');
const path = require('path');
const paths = require('../config/paths');

var overrideConfig;
if ( overrideArg ) {
overrideConfig = require(path.resolve(paths.appSrc, '..', overrideArg.split('=')[1] ));
}

argv.push('--config', JSON.stringify(createJestConfig(
relativePath => path.resolve(__dirname, '..', relativePath),
path.resolve(paths.appSrc, '..'),
false
false,
overrideConfig
)));
// @remove-on-eject-end
jest.run(argv);
3 changes: 3 additions & 0 deletions packages/react-scripts/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ You will also see any lint errors in the console.
Launches the test runner in the interactive watch mode.<br>
See the section about [running tests](#running-tests) for more information.

You can add your own test configuration by specifying a json file using '--overrideConfig'.
This will also override existing react-scripts configuration.

### `npm run build`

Builds the app for production to the `build` folder.<br>
Expand Down
9 changes: 8 additions & 1 deletion packages/react-scripts/utils/createJestConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
const fs = require('fs');
const paths = require('../config/paths');

module.exports = (resolve, rootDir, isEjecting) => {
module.exports = (resolve, rootDir, isEjecting, overrides) => {
// Use this instead of `paths.testsSetup` to avoid putting
// an absolute filename into configuration after ejecting.
const setupTestsFile = fs.existsSync(paths.testsSetup) ? '<rootDir>/src/setupTests.js' : undefined;
Expand Down Expand Up @@ -45,5 +45,12 @@ module.exports = (resolve, rootDir, isEjecting) => {
if (rootDir) {
config.rootDir = rootDir;
}

if( overrides ) {
Object.keys(overrides).forEach((k) => {
config[k] = overrides[k];
});
}

return config;
};