Skip to content

Commit 6be8ae0

Browse files
gaearonfeiqitian
authored andcommitted
Update Jest (facebook#530)
* Update Jest * Remove default snapshot test * Fix a few things * Add a simple default test * App.spec.js -> App.test.js * Fix e2e test * Bump Jest
1 parent f994f3b commit 6be8ae0

File tree

9 files changed

+51
-64
lines changed

9 files changed

+51
-64
lines changed

config/jest/environment.js

-7
This file was deleted.

package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"create-react-app": "node global-cli/index.js --scripts-version \"$PWD/`npm pack`\"",
1616
"e2e": "tasks/e2e.sh",
1717
"start": "node scripts/start.js --debug-template",
18-
"test": "node scripts/test.js --debug-template"
18+
"test": "node scripts/test.js --debug-template --watch --env=jsdom"
1919
},
2020
"files": [
2121
"PATENTS",
@@ -31,7 +31,7 @@
3131
"autoprefixer": "6.4.0",
3232
"babel-core": "6.14.0",
3333
"babel-eslint": "6.1.2",
34-
"babel-jest": "14.1.0",
34+
"babel-jest": "15.0.0",
3535
"babel-loader": "6.2.5",
3636
"babel-plugin-transform-class-properties": "6.11.5",
3737
"babel-plugin-transform-object-rest-spread": "6.8.0",
@@ -61,7 +61,7 @@
6161
"html-loader": "0.4.3",
6262
"html-webpack-plugin": "2.22.0",
6363
"http-proxy-middleware": "0.17.0",
64-
"jest": "14.1.0",
64+
"jest": "15.0.1",
6565
"json-loader": "0.5.4",
6666
"object-assign": "4.1.0",
6767
"opn": "4.0.2",
@@ -80,8 +80,7 @@
8080
"devDependencies": {
8181
"bundle-deps": "1.0.0",
8282
"react": "^15.3.0",
83-
"react-dom": "^15.3.0",
84-
"react-test-renderer": "^15.3.0"
83+
"react-dom": "^15.3.0"
8584
},
8685
"optionalDependencies": {
8786
"fsevents": "1.0.14"

scripts/eject.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ prompt(
4141
path.join('config', 'webpack.config.prod.js'),
4242
path.join('config', 'jest', 'CSSStub.js'),
4343
path.join('config', 'jest', 'FileStub.js'),
44-
path.join('config', 'jest', 'environment.js'),
4544
path.join('config', 'jest', 'transform.js'),
4645
path.join('scripts', 'build.js'),
4746
path.join('scripts', 'start.js'),
@@ -99,17 +98,19 @@ prompt(
9998
});
10099

101100
console.log('Updating scripts');
101+
delete appPackage.scripts['eject'];
102102
Object.keys(appPackage.scripts).forEach(function (key) {
103-
appPackage.scripts[key] = 'node ./scripts/' + key + '.js'
103+
appPackage.scripts[key] = appPackage.scripts[key]
104+
.replace(/react-scripts test/g, 'jest')
105+
.replace(/react-scripts (\w+)/g, 'node scripts/$1.js');
104106
});
105-
delete appPackage.scripts['eject'];
106107

107-
appPackage.scripts.test = 'jest';
108+
// Add Jest config
108109
appPackage.jest = createJestConfig(
109110
filePath => path.join('<rootDir>', filePath)
110111
);
111112

112-
// explicitly specify ESLint config path for editor plugins
113+
// Explicitly specify ESLint config path for editor plugins
113114
appPackage.eslintConfig = {
114115
extends: './config/eslint.js',
115116
};

scripts/init.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ module.exports = function(appPath, appName, verbose, originalDirectory) {
2323
appPackage.devDependencies = appPackage.devDependencies || {};
2424

2525
// Setup the script rules
26-
appPackage.scripts = {};
27-
['start', 'build', 'eject', 'test'].forEach(function(command) {
28-
appPackage.scripts[command] = 'react-scripts ' + command;
29-
});
26+
appPackage.scripts = {
27+
'start': 'react-scripts start',
28+
'build': 'react-scripts build',
29+
'test': 'react-scripts test --watch --env=jsdom',
30+
'eject': 'react-scripts eject'
31+
};
3032

3133
// explicitly specify ESLint config path for editor plugins
3234
appPackage.eslintConfig = {
@@ -69,7 +71,6 @@ module.exports = function(appPath, appName, verbose, originalDirectory) {
6971
'install',
7072
'react',
7173
'react-dom',
72-
'react-test-renderer',
7374
'--save',
7475
verbose && '--verbose'
7576
].filter(function(e) { return e; });

scripts/test.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const paths = require('../config/paths');
1616

1717
const argv = process.argv.slice(2);
1818

19-
const index = argv.indexOf('--debug-template');
20-
if (index !== -1) {
21-
argv.splice(index, 1);
19+
// Don't pass this option to Jest
20+
const debugTemplateIndex = argv.indexOf('--debug-template');
21+
if (debugTemplateIndex !== -1) {
22+
argv.splice(debugTemplateIndex, 1);
2223
}
2324

2425
argv.push('--config', JSON.stringify(createJestConfig(

scripts/utils/createJestConfig.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,14 @@
99

1010
module.exports = (resolve, rootDir) => {
1111
const config = {
12-
automock: false,
1312
moduleNameMapper: {
1413
'^[./a-zA-Z0-9$_-]+\\.(jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm)$': resolve('config/jest/FileStub.js'),
1514
'^[./a-zA-Z0-9$_-]+\\.css$': resolve('config/jest/CSSStub.js')
1615
},
17-
persistModuleRegistryBetweenSpecs: true,
1816
scriptPreprocessor: resolve('config/jest/transform.js'),
19-
setupFiles: [
20-
resolve('config/polyfills.js')
21-
],
22-
setupTestFrameworkScriptFile: resolve('config/jest/environment.js'),
23-
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/build/'],
24-
// Allow three popular conventions:
25-
// **/__tests__/*.js
26-
// **/*.test.js
27-
// **/*.spec.js
28-
testRegex: '(__tests__/.*|\\.(test|spec))\\.js$',
29-
testEnvironment: 'node',
30-
verbose: true
17+
setupFiles: [resolve('config/polyfills.js')],
18+
testPathIgnorePatterns: ['<rootDir>/(build|docs|node_modules)/'],
19+
testEnvironment: 'node'
3120
};
3221
if (rootDir) {
3322
config.rootDir = rootDir;

tasks/e2e.sh

+18-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ cd "$(dirname "$0")"
1212
function cleanup {
1313
echo 'Cleaning up.'
1414
cd $initial_path
15-
rm ../template/src/__tests__/__snapshots__/App-test.js.snap
15+
# Uncomment when snapshot testing is enabled by default:
16+
# rm ../template/src/__snapshots__/App.test.js.snap
1617
rm -rf $temp_cli_path $temp_app_path
1718
}
1819

@@ -53,12 +54,9 @@ perl -i -p0e 's/bundledDependencies.*?]/bundledDependencies": []/s' package.json
5354
npm install
5455
scripts_path=$PWD/`npm pack`
5556

56-
# lint
57+
# Lint
5758
./node_modules/.bin/eslint --ignore-path .gitignore ./
5859

59-
# Test local start command
60-
npm start -- --smoke-test
61-
6260
# Test local build command
6361
npm run build
6462

@@ -69,9 +67,13 @@ test -e build/static/css/*.css
6967
test -e build/static/media/*.svg
7068
test -e build/favicon.ico
7169

72-
# Run tests
73-
npm run test
74-
test -e template/src/__tests__/__snapshots__/App-test.js.snap
70+
# Run tests, overriding watch option to disable it
71+
npm test -- --watch=no
72+
# Uncomment when snapshot testing is enabled by default:
73+
# test -e template/src/__snapshots__/App.test.js.snap
74+
75+
# Test local start command
76+
npm start -- --smoke-test
7577

7678
# Pack CLI
7779
cd global-cli
@@ -99,9 +101,10 @@ test -e build/static/css/*.css
99101
test -e build/static/media/*.svg
100102
test -e build/favicon.ico
101103

102-
# Run tests
103-
npm run test
104-
test -e src/__tests__/__snapshots__/App-test.js.snap
104+
# Run tests, overriding watch option to disable it
105+
npm test -- --watch=no
106+
# Uncomment when snapshot testing is enabled by default:
107+
# test -e src/__snapshots__/App.test.js.snap
105108

106109
# Test the server
107110
npm start -- --smoke-test
@@ -117,9 +120,10 @@ test -e build/static/css/*.css
117120
test -e build/static/media/*.svg
118121
test -e build/favicon.ico
119122

120-
# Run tests
121-
npm run test
122-
test -e src/__tests__/__snapshots__/App-test.js.snap
123+
# Run tests, overriding watch option to disable it
124+
npm test -- --watch=no
125+
# Uncomment when snapshot testing is enabled by default:
126+
# test -e src/__snapshots__/App.test.js.snap
123127

124128
# Test the server
125129
npm start -- --smoke-test

template/src/App.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
5+
describe('App', () => {
6+
it('renders without crashing', () => {
7+
const div = document.createElement('div');
8+
ReactDOM.render(<App />, div);
9+
});
10+
});

template/src/__tests__/App-test.js

-11
This file was deleted.

0 commit comments

Comments
 (0)