Skip to content

Commit a94b252

Browse files
gaelolliviergaearon
authored andcommitted
Add src/setupTests.js to specify environment setup for Jest (facebook#545) (facebook#548)
1 parent 88ed883 commit a94b252

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

config/paths.js

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module.exports = {
3737
appHtml: resolveApp('index.html'),
3838
appPackageJson: resolveApp('package.json'),
3939
appSrc: resolveApp('src'),
40+
testsSetup: resolveApp('src/setupTests.js'),
4041
appNodeModules: resolveApp('node_modules'),
4142
ownNodeModules: resolveApp('node_modules'),
4243
nodePaths: nodePaths
@@ -53,6 +54,7 @@ module.exports = {
5354
appHtml: resolveApp('index.html'),
5455
appPackageJson: resolveApp('package.json'),
5556
appSrc: resolveApp('src'),
57+
testsSetup: resolveApp('src/setupTests.js'),
5658
appNodeModules: resolveApp('node_modules'),
5759
// this is empty with npm3 but node resolution searches higher anyway:
5860
ownNodeModules: resolveOwn('../node_modules'),
@@ -66,6 +68,7 @@ module.exports = {
6668
appHtml: resolveOwn('../template/index.html'),
6769
appPackageJson: resolveOwn('../package.json'),
6870
appSrc: resolveOwn('../template/src'),
71+
testsSetup: resolveOwn('../template/src/setupTests.js'),
6972
appNodeModules: resolveOwn('../node_modules'),
7073
ownNodeModules: resolveOwn('../node_modules'),
7174
nodePaths: nodePaths

scripts/utils/createJestConfig.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@
99
*/
1010
// @remove-on-eject-end
1111

12+
const pathExists = require('path-exists');
13+
const paths = require('../../config/paths');
14+
1215
module.exports = (resolve, rootDir) => {
16+
const setupFiles = [resolve('config/polyfills.js')];
17+
if (pathExists.sync(paths.testsSetup)) {
18+
setupFiles.push(paths.testsSetup);
19+
}
20+
1321
const config = {
1422
moduleNameMapper: {
1523
'^[./a-zA-Z0-9$_-]+\\.(jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm)$': resolve('config/jest/FileStub.js'),
1624
'^[./a-zA-Z0-9$_-]+\\.css$': resolve('config/jest/CSSStub.js')
1725
},
1826
scriptPreprocessor: resolve('config/jest/transform.js'),
19-
setupFiles: [resolve('config/polyfills.js')],
27+
setupFiles: setupFiles,
2028
testPathIgnorePatterns: ['<rootDir>/(build|docs|node_modules)/'],
2129
testEnvironment: 'node'
2230
};

template/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ You can find the most recent version of this guide [here](https://github.com/fac
3232
- [Writing Tests](#writing-tests)
3333
- [Testing Components](#testing-components)
3434
- [Using Third Party Assertion Libraries](#using-third-party-assertion-libraries)
35+
- [Initializing Test Environment](#initializing-test-environment)
3536
- [Focusing and Excluding Tests](#focusing-and-excluding-tests)
3637
- [Coverage Reporting](#coverage-reporting)
3738
- [Continuous Integration](#continuous-integration)
@@ -674,6 +675,24 @@ import { expect } from 'chai';
674675
675676
and then use them in your tests like you normally do.
676677
678+
### Initializing Test Environment
679+
680+
>Note: this feature is available with `react-scripts@0.4.0` and higher.
681+
682+
If your app uses a browser API that you need to mock in your tests or if you just need a global setup before running your tests, add a `src/setupTests.js` to your project. It will be automatically executed before running your tests.
683+
684+
For example:
685+
686+
#### `src/setupTests.js`
687+
```js
688+
const localStorageMock = {
689+
getItem: jest.fn(),
690+
setItem: jest.fn(),
691+
clear: jest.fn()
692+
};
693+
global.localStorage = localStorageMock
694+
```
695+
677696
### Focusing and Excluding Tests
678697
679698
You can replace `it()` with `xit()` to temporarily exclude a test from being executed.

0 commit comments

Comments
 (0)