Skip to content

Commit 367e2b8

Browse files
committed
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
1 parent d721d4a commit 367e2b8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module.exports = (resolve, rootDir, isEjecting) => {
1919
? '<rootDir>/src/setupTests.js'
2020
: undefined;
2121

22+
const appJestConfig = require(paths.appPackageJson).jest;
23+
2224
// TODO: I don't know if it's safe or not to just use / as path separator
2325
// in Jest configs. We need help from somebody with Windows to determine this.
2426
const config = {
@@ -45,5 +47,10 @@ module.exports = (resolve, rootDir, isEjecting) => {
4547
if (rootDir) {
4648
config.rootDir = rootDir;
4749
}
48-
return config;
50+
51+
if (Object.prototype.toString.call(appJestConfig) === '[object Object]') {
52+
return Object.assign({}, config, appJestConfig);
53+
} else {
54+
return config;
55+
}
4956
};

packages/react-scripts/template/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
4646
- [Pre-Rendering into Static HTML Files](#pre-rendering-into-static-html-files)
4747
- [Injecting Data from the Server into the Page](#injecting-data-from-the-server-into-the-page)
4848
- [Running Tests](#running-tests)
49+
- [Configuring Jest](#configuring-jest)
4950
- [Filename Conventions](#filename-conventions)
5051
- [Command Line Interface](#command-line-interface)
5152
- [Version Control Integration](#version-control-integration)
@@ -924,6 +925,20 @@ While Jest provides browser globals such as `window` thanks to [jsdom](https://g
924925

925926
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.
926927

928+
929+
### Configuring Jest
930+
931+
To override default configurations of Jest, simply add a `jest` field to your `package.json`, for example:
932+
933+
```js
934+
"jest": {
935+
"verbose": true,
936+
"testResultsProcessor": "./node_modules/jest-junit"
937+
},
938+
```
939+
940+
See [Jest Configuration](https://facebook.github.io/jest/docs/configuration.html) for more.
941+
927942
### Filename Conventions
928943

929944
Jest will look for test files with any of the following popular naming conventions:

0 commit comments

Comments
 (0)