Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

Commit 0e5abad

Browse files
cpojergaearon
authored andcommitted
Add Jest. (#250)
1 parent c7c1f51 commit 0e5abad

File tree

15 files changed

+149
-9
lines changed

15 files changed

+149
-9
lines changed

Diff for: .eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ const clientESLintConfig = require('./config/eslint');
22

33
module.exports = Object.assign({}, clientESLintConfig, {
44
env: Object.assign({}, clientESLintConfig.env, {
5-
node: true
5+
node: true,
66
})
77
});

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ build
33
.DS_Store
44
*.tgz
55
my-app*
6+
template/src/__tests__/__snapshots__/

Diff for: bin/react-scripts.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ var args = process.argv.slice(3);
55

66
switch (script) {
77
case 'build':
8-
case 'start':
98
case 'eject':
9+
case 'start':
10+
case 'test':
1011
var result = spawn.sync(
1112
'node',
1213
[require.resolve('../scripts/' + script)].concat(args),

Diff for: config/eslint.js

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = {
2828
browser: true,
2929
commonjs: true,
3030
es6: true,
31+
jest: true,
3132
node: true
3233
},
3334

Diff for: config/jest/CSSStub.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @flow
9+
*/
10+
11+
module.exports = {};

Diff for: config/jest/FileStub.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*
8+
* @flow
9+
*/
10+
11+
module.exports = "test-file-stub";

Diff for: config/jest/transform.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the BSD-style license found in the
5+
* LICENSE file in the root directory of this source tree. An additional grant
6+
* of patent rights can be found in the PATENTS file in the same directory.
7+
*/
8+
9+
const babelDev = require('../babel.dev');
10+
const babelJest = require('babel-jest');
11+
12+
module.exports = babelJest.createTransformer(babelDev);

Diff for: global-cli/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ function createApp(name, verbose, version) {
8787
version: '0.0.1',
8888
private: true,
8989
};
90-
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson));
90+
fs.writeFileSync(
91+
path.join(root, 'package.json'),
92+
JSON.stringify(packageJson, null, 2)
93+
);
9194
var originalDirectory = process.cwd();
9295
process.chdir(root);
9396

Diff for: package.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
"url": "https://github.com/facebookincubator/create-react-app/issues"
1212
},
1313
"scripts": {
14-
"start": "node scripts/start.js --debug-template",
1514
"build": "node scripts/build.js --debug-template",
1615
"create-react-app": "node global-cli/index.js --scripts-version \"$PWD/`npm pack`\"",
17-
"test": "tasks/e2e.sh"
16+
"e2e": "tasks/e2e.sh",
17+
"start": "node scripts/start.js --debug-template",
18+
"test": "node scripts/test.js --debug-template"
1819
},
1920
"files": [
2021
"PATENTS",
@@ -30,6 +31,7 @@
3031
"autoprefixer": "6.3.7",
3132
"babel-core": "6.11.4",
3233
"babel-eslint": "6.1.2",
34+
"babel-jest": "14.1.0",
3335
"babel-loader": "6.2.4",
3436
"babel-plugin-syntax-trailing-function-commas": "6.8.0",
3537
"babel-plugin-transform-class-properties": "6.11.5",
@@ -57,6 +59,7 @@
5759
"fs-extra": "0.30.0",
5860
"gzip-size": "3.0.0",
5961
"html-webpack-plugin": "2.22.0",
62+
"jest": "14.1.0",
6063
"json-loader": "0.5.4",
6164
"opn": "4.0.2",
6265
"postcss-loader": "0.9.1",
@@ -70,8 +73,9 @@
7073
},
7174
"devDependencies": {
7275
"bundle-deps": "1.0.0",
73-
"react": "^15.2.1",
74-
"react-dom": "^15.2.1"
76+
"react": "^15.3.0",
77+
"react-dom": "^15.3.0",
78+
"react-test-renderer": "^15.3.0"
7579
},
7680
"optionalDependencies": {
7781
"fsevents": "1.0.14"

Diff for: scripts/eject.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*/
99

10+
var createJestConfig = require('./utils/create-jest-config');
1011
var fs = require('fs');
1112
var path = require('path');
13+
var prompt = require('./utils/prompt');
1214
var rimrafSync = require('rimraf').sync;
1315
var spawnSync = require('cross-spawn').sync;
14-
var prompt = require('./utils/prompt');
1516

1617
prompt(
1718
'Are you sure you want to eject? This action is permanent.',
@@ -37,6 +38,9 @@ prompt(
3738
path.join('config', 'polyfills.js'),
3839
path.join('config', 'webpack.config.dev.js'),
3940
path.join('config', 'webpack.config.prod.js'),
41+
path.join('config', 'jest', 'CSSStub.js'),
42+
path.join('config', 'jest', 'FileStub.js'),
43+
path.join('config', 'jest', 'transform.js'),
4044
path.join('scripts', 'build.js'),
4145
path.join('scripts', 'start.js'),
4246
path.join('scripts', 'utils', 'chrome.applescript'),
@@ -59,6 +63,7 @@ prompt(
5963
// Copy the files over
6064
fs.mkdirSync(path.join(appPath, 'config'));
6165
fs.mkdirSync(path.join(appPath, 'config', 'flow'));
66+
fs.mkdirSync(path.join(appPath, 'config', 'jest'));
6267
fs.mkdirSync(path.join(appPath, 'scripts'));
6368
fs.mkdirSync(path.join(appPath, 'scripts', 'utils'));
6469

@@ -96,6 +101,11 @@ prompt(
96101
});
97102
delete appPackage.scripts['eject'];
98103

104+
appPackage.scripts.test = 'jest';
105+
appPackage.jest = createJestConfig(
106+
filePath => path.join('<rootDir>', filePath)
107+
);
108+
99109
// explicitly specify ESLint config path for editor plugins
100110
appPackage.eslintConfig = {
101111
extends: './config/eslint.js',

Diff for: scripts/init.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ module.exports = function(appPath, appName, verbose, originalDirectory) {
1919

2020
// Copy over some of the devDependencies
2121
appPackage.dependencies = appPackage.dependencies || {};
22+
appPackage.devDependencies = appPackage.devDependencies || {};
2223
['react', 'react-dom'].forEach(function (key) {
2324
appPackage.dependencies[key] = ownPackage.devDependencies[key];
2425
});
26+
['react-test-renderer'].forEach(function (key) {
27+
appPackage.devDependencies[key] = ownPackage.devDependencies[key];
28+
});
2529

2630
// Setup the script rules
2731
appPackage.scripts = {};
28-
['start', 'build', 'eject'].forEach(function(command) {
32+
['start', 'build', 'eject', 'test'].forEach(function(command) {
2933
appPackage.scripts[command] = 'react-scripts ' + command;
3034
});
3135

Diff for: scripts/test.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
process.env.NODE_ENV = 'test';
11+
12+
const createJestConfig = require('./utils/create-jest-config');
13+
const jest = require('jest');
14+
const path = require('path');
15+
const paths = require('../config/paths');
16+
17+
const argv = process.argv.slice(2);
18+
19+
const index = argv.indexOf('--debug-template');
20+
if (index !== -1) {
21+
argv.splice(index, 1);
22+
}
23+
24+
argv.push('--config', JSON.stringify(createJestConfig(
25+
relativePath => path.resolve(__dirname, '..', relativePath),
26+
path.resolve(paths.appSrc, '..')
27+
)));
28+
29+
jest.run(argv);

Diff for: scripts/utils/create-jest-config.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
module.exports = (resolve, rootDir) => {
11+
const config = {
12+
automock: false,
13+
moduleNameMapper: {
14+
'^[./a-zA-Z0-9$_-]+\\.(jpg|png|gif|eot|svg|ttf|woff|woff2|mp4|webm)$': resolve('config/jest/FileStub.js'),
15+
'^[./a-zA-Z0-9$_-]+\\.css$': resolve('config/jest/CSSStub.js')
16+
},
17+
persistModuleRegistryBetweenSpecs: true,
18+
scriptPreprocessor: resolve('config/jest/transform.js'),
19+
setupFiles: [
20+
resolve('config/polyfills.js')
21+
],
22+
testEnvironment: 'node'
23+
};
24+
if (rootDir) {
25+
config.rootDir = rootDir;
26+
}
27+
return config;
28+
};

Diff for: tasks/e2e.sh

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ cd "$(dirname "$0")"
1111
function cleanup {
1212
echo 'Cleaning up.'
1313
cd $initial_path
14+
rm ../template/src/__tests__/__snapshots__/App-test.js.snap
1415
rm -rf $temp_cli_path $temp_app_path
1516
}
1617

@@ -38,6 +39,7 @@ set -x
3839

3940
# npm pack the two directories to make sure they are valid npm modules
4041
initial_path=$PWD
42+
4143
cd ..
4244

4345
# A hacky way to avoid bundling dependencies.
@@ -65,6 +67,10 @@ test -e build/static/js/*.js
6567
test -e build/static/css/*.css
6668
test -e build/static/media/*.svg
6769

70+
# Run tests
71+
npm run test
72+
test -e template/src/__tests__/__snapshots__/App-test.js.snap
73+
6874
# Pack CLI
6975
cd global-cli
7076
npm install
@@ -90,6 +96,10 @@ test -e build/static/js/*.js
9096
test -e build/static/css/*.css
9197
test -e build/static/media/*.svg
9298

99+
# Run tests
100+
npm run test
101+
test -e src/__tests__/__snapshots__/App-test.js.snap
102+
93103
# Test the server
94104
npm start -- --smoke-test
95105

@@ -103,6 +113,10 @@ test -e build/static/js/*.js
103113
test -e build/static/css/*.css
104114
test -e build/static/media/*.svg
105115

116+
# Run tests
117+
npm run test
118+
test -e src/__tests__/__snapshots__/App-test.js.snap
119+
106120
# Test the server
107121
npm start -- --smoke-test
108122

Diff for: template/src/__tests__/App-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
import App from '../App';
3+
import renderer from 'react-test-renderer';
4+
5+
describe('App', () => {
6+
it('renders a welcome view', () => {
7+
const instance = renderer.create(<App />);
8+
const tree = instance.toJSON();
9+
expect(tree).toMatchSnapshot();
10+
});
11+
});

0 commit comments

Comments
 (0)