From 367e2b819c9334fd4814c6f9a062b909093ed282 Mon Sep 17 00:00:00 2001 From: danjamin Date: Mon, 17 Apr 2017 16:08:03 -0700 Subject: [PATCH] Added ability to configure Jest via package.json (#1785) - works when NOT ejected by merging default and app Jest options - eject simply combines app and default into a single package.json jest field --- .../scripts/utils/createJestConfig.js | 9 ++++++++- packages/react-scripts/template/README.md | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/react-scripts/scripts/utils/createJestConfig.js b/packages/react-scripts/scripts/utils/createJestConfig.js index 5ba7fd16cb2..411b714235d 100644 --- a/packages/react-scripts/scripts/utils/createJestConfig.js +++ b/packages/react-scripts/scripts/utils/createJestConfig.js @@ -19,6 +19,8 @@ module.exports = (resolve, rootDir, isEjecting) => { ? '/src/setupTests.js' : undefined; + const appJestConfig = require(paths.appPackageJson).jest; + // TODO: I don't know if it's safe or not to just use / as path separator // in Jest configs. We need help from somebody with Windows to determine this. const config = { @@ -45,5 +47,10 @@ module.exports = (resolve, rootDir, isEjecting) => { if (rootDir) { config.rootDir = rootDir; } - return config; + + if (Object.prototype.toString.call(appJestConfig) === '[object Object]') { + return Object.assign({}, config, appJestConfig); + } else { + return config; + } }; diff --git a/packages/react-scripts/template/README.md b/packages/react-scripts/template/README.md index 0cda7e071f6..d4a3558a2be 100644 --- a/packages/react-scripts/template/README.md +++ b/packages/react-scripts/template/README.md @@ -46,6 +46,7 @@ You can find the most recent version of this guide [here](https://github.com/fac - [Pre-Rendering into Static HTML Files](#pre-rendering-into-static-html-files) - [Injecting Data from the Server into the Page](#injecting-data-from-the-server-into-the-page) - [Running Tests](#running-tests) + - [Configuring Jest](#configuring-jest) - [Filename Conventions](#filename-conventions) - [Command Line Interface](#command-line-interface) - [Version Control Integration](#version-control-integration) @@ -924,6 +925,20 @@ While Jest provides browser globals such as `window` thanks to [jsdom](https://g We recommend that you use a separate tool for browser end-to-end tests if you need them. They are beyond the scope of Create React App. + +### Configuring Jest + +To override default configurations of Jest, simply add a `jest` field to your `package.json`, for example: + +```js +"jest": { + "verbose": true, + "testResultsProcessor": "./node_modules/jest-junit" +}, +``` + +See [Jest Configuration](https://facebook.github.io/jest/docs/configuration.html) for more. + ### Filename Conventions Jest will look for test files with any of the following popular naming conventions: