Commit 67fe7592 authored by Dan Abramov's avatar Dan Abramov Committed by GitHub
Browse files

Update Jest (#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
parent 497371b6
Showing with 51 additions and 64 deletions
+51 -64
// Currently, Jest mocks setTimeout() and similar functions by default:
// https://facebook.github.io/jest/docs/timer-mocks.html
// We think this is confusing, so we disable this feature.
// If you see value in it, run `jest.useFakeTimers()` in individual tests.
beforeEach(() => {
jest.useRealTimers();
});
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
"create-react-app": "node global-cli/index.js --scripts-version \"$PWD/`npm pack`\"", "create-react-app": "node global-cli/index.js --scripts-version \"$PWD/`npm pack`\"",
"e2e": "tasks/e2e.sh", "e2e": "tasks/e2e.sh",
"start": "node scripts/start.js --debug-template", "start": "node scripts/start.js --debug-template",
"test": "node scripts/test.js --debug-template" "test": "node scripts/test.js --debug-template --watch --env=jsdom"
}, },
"files": [ "files": [
"PATENTS", "PATENTS",
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
"autoprefixer": "6.4.0", "autoprefixer": "6.4.0",
"babel-core": "6.14.0", "babel-core": "6.14.0",
"babel-eslint": "6.1.2", "babel-eslint": "6.1.2",
"babel-jest": "14.1.0", "babel-jest": "15.0.0",
"babel-loader": "6.2.5", "babel-loader": "6.2.5",
"babel-plugin-transform-class-properties": "6.11.5", "babel-plugin-transform-class-properties": "6.11.5",
"babel-plugin-transform-object-rest-spread": "6.8.0", "babel-plugin-transform-object-rest-spread": "6.8.0",
...@@ -61,7 +61,7 @@ ...@@ -61,7 +61,7 @@
"html-loader": "0.4.3", "html-loader": "0.4.3",
"html-webpack-plugin": "2.22.0", "html-webpack-plugin": "2.22.0",
"http-proxy-middleware": "0.17.0", "http-proxy-middleware": "0.17.0",
"jest": "14.1.0", "jest": "15.0.1",
"json-loader": "0.5.4", "json-loader": "0.5.4",
"object-assign": "4.1.0", "object-assign": "4.1.0",
"opn": "4.0.2", "opn": "4.0.2",
...@@ -80,8 +80,7 @@ ...@@ -80,8 +80,7 @@
"devDependencies": { "devDependencies": {
"bundle-deps": "1.0.0", "bundle-deps": "1.0.0",
"react": "^15.3.0", "react": "^15.3.0",
"react-dom": "^15.3.0", "react-dom": "^15.3.0"
"react-test-renderer": "^15.3.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"fsevents": "1.0.14" "fsevents": "1.0.14"
......
...@@ -41,7 +41,6 @@ prompt( ...@@ -41,7 +41,6 @@ prompt(
path.join('config', 'webpack.config.prod.js'), path.join('config', 'webpack.config.prod.js'),
path.join('config', 'jest', 'CSSStub.js'), path.join('config', 'jest', 'CSSStub.js'),
path.join('config', 'jest', 'FileStub.js'), path.join('config', 'jest', 'FileStub.js'),
path.join('config', 'jest', 'environment.js'),
path.join('config', 'jest', 'transform.js'), path.join('config', 'jest', 'transform.js'),
path.join('scripts', 'build.js'), path.join('scripts', 'build.js'),
path.join('scripts', 'start.js'), path.join('scripts', 'start.js'),
...@@ -99,17 +98,19 @@ prompt( ...@@ -99,17 +98,19 @@ prompt(
}); });
console.log('Updating scripts'); console.log('Updating scripts');
delete appPackage.scripts['eject'];
Object.keys(appPackage.scripts).forEach(function (key) { Object.keys(appPackage.scripts).forEach(function (key) {
appPackage.scripts[key] = 'node ./scripts/' + key + '.js' appPackage.scripts[key] = appPackage.scripts[key]
.replace(/react-scripts test/g, 'jest')
.replace(/react-scripts (\w+)/g, 'node scripts/$1.js');
}); });
delete appPackage.scripts['eject'];
appPackage.scripts.test = 'jest'; // Add Jest config
appPackage.jest = createJestConfig( appPackage.jest = createJestConfig(
filePath => path.join('<rootDir>', filePath) filePath => path.join('<rootDir>', filePath)
); );
// explicitly specify ESLint config path for editor plugins // Explicitly specify ESLint config path for editor plugins
appPackage.eslintConfig = { appPackage.eslintConfig = {
extends: './config/eslint.js', extends: './config/eslint.js',
}; };
......
...@@ -23,10 +23,12 @@ module.exports = function(appPath, appName, verbose, originalDirectory) { ...@@ -23,10 +23,12 @@ module.exports = function(appPath, appName, verbose, originalDirectory) {
appPackage.devDependencies = appPackage.devDependencies || {}; appPackage.devDependencies = appPackage.devDependencies || {};
// Setup the script rules // Setup the script rules
appPackage.scripts = {}; appPackage.scripts = {
['start', 'build', 'eject', 'test'].forEach(function(command) { 'start': 'react-scripts start',
appPackage.scripts[command] = 'react-scripts ' + command; 'build': 'react-scripts build',
}); 'test': 'react-scripts test --watch --env=jsdom',
'eject': 'react-scripts eject'
};
// explicitly specify ESLint config path for editor plugins // explicitly specify ESLint config path for editor plugins
appPackage.eslintConfig = { appPackage.eslintConfig = {
...@@ -69,7 +71,6 @@ module.exports = function(appPath, appName, verbose, originalDirectory) { ...@@ -69,7 +71,6 @@ module.exports = function(appPath, appName, verbose, originalDirectory) {
'install', 'install',
'react', 'react',
'react-dom', 'react-dom',
'react-test-renderer',
'--save', '--save',
verbose && '--verbose' verbose && '--verbose'
].filter(function(e) { return e; }); ].filter(function(e) { return e; });
......
...@@ -16,9 +16,10 @@ const paths = require('../config/paths'); ...@@ -16,9 +16,10 @@ const paths = require('../config/paths');
const argv = process.argv.slice(2); const argv = process.argv.slice(2);
const index = argv.indexOf('--debug-template'); // Don't pass this option to Jest
if (index !== -1) { const debugTemplateIndex = argv.indexOf('--debug-template');
argv.splice(index, 1); if (debugTemplateIndex !== -1) {
argv.splice(debugTemplateIndex, 1);
} }
argv.push('--config', JSON.stringify(createJestConfig( argv.push('--config', JSON.stringify(createJestConfig(
......
...@@ -9,25 +9,14 @@ ...@@ -9,25 +9,14 @@
module.exports = (resolve, rootDir) => { module.exports = (resolve, rootDir) => {
const config = { const config = {
automock: false,
moduleNameMapper: { moduleNameMapper: {
'^[./a-zA-Z0-9$_-]+\\.(jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm)$': resolve('config/jest/FileStub.js'), '^[./a-zA-Z0-9$_-]+\\.(jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm)$': resolve('config/jest/FileStub.js'),
'^[./a-zA-Z0-9$_-]+\\.css$': resolve('config/jest/CSSStub.js') '^[./a-zA-Z0-9$_-]+\\.css$': resolve('config/jest/CSSStub.js')
}, },
persistModuleRegistryBetweenSpecs: true,
scriptPreprocessor: resolve('config/jest/transform.js'), scriptPreprocessor: resolve('config/jest/transform.js'),
setupFiles: [ setupFiles: [resolve('config/polyfills.js')],
resolve('config/polyfills.js') testPathIgnorePatterns: ['<rootDir>/(build|docs|node_modules)/'],
], testEnvironment: 'node'
setupTestFrameworkScriptFile: resolve('config/jest/environment.js'),
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/build/'],
// Allow three popular conventions:
// **/__tests__/*.js
// **/*.test.js
// **/*.spec.js
testRegex: '(__tests__/.*|\\.(test|spec))\\.js$',
testEnvironment: 'node',
verbose: true
}; };
if (rootDir) { if (rootDir) {
config.rootDir = rootDir; config.rootDir = rootDir;
......
...@@ -12,7 +12,8 @@ cd "$(dirname "$0")" ...@@ -12,7 +12,8 @@ cd "$(dirname "$0")"
function cleanup { function cleanup {
echo 'Cleaning up.' echo 'Cleaning up.'
cd $initial_path cd $initial_path
rm ../template/src/__tests__/__snapshots__/App-test.js.snap # Uncomment when snapshot testing is enabled by default:
# rm ../template/src/__snapshots__/App.test.js.snap
rm -rf $temp_cli_path $temp_app_path rm -rf $temp_cli_path $temp_app_path
} }
...@@ -53,12 +54,9 @@ perl -i -p0e 's/bundledDependencies.*?]/bundledDependencies": []/s' package.json ...@@ -53,12 +54,9 @@ perl -i -p0e 's/bundledDependencies.*?]/bundledDependencies": []/s' package.json
npm install npm install
scripts_path=$PWD/`npm pack` scripts_path=$PWD/`npm pack`
# lint # Lint
./node_modules/.bin/eslint --ignore-path .gitignore ./ ./node_modules/.bin/eslint --ignore-path .gitignore ./
# Test local start command
npm start -- --smoke-test
# Test local build command # Test local build command
npm run build npm run build
...@@ -69,9 +67,13 @@ test -e build/static/css/*.css ...@@ -69,9 +67,13 @@ test -e build/static/css/*.css
test -e build/static/media/*.svg test -e build/static/media/*.svg
test -e build/favicon.ico test -e build/favicon.ico
# Run tests # Run tests, overriding watch option to disable it
npm run test npm test -- --watch=no
test -e template/src/__tests__/__snapshots__/App-test.js.snap # Uncomment when snapshot testing is enabled by default:
# test -e template/src/__snapshots__/App.test.js.snap
# Test local start command
npm start -- --smoke-test
# Pack CLI # Pack CLI
cd global-cli cd global-cli
...@@ -99,9 +101,10 @@ test -e build/static/css/*.css ...@@ -99,9 +101,10 @@ test -e build/static/css/*.css
test -e build/static/media/*.svg test -e build/static/media/*.svg
test -e build/favicon.ico test -e build/favicon.ico
# Run tests # Run tests, overriding watch option to disable it
npm run test npm test -- --watch=no
test -e src/__tests__/__snapshots__/App-test.js.snap # Uncomment when snapshot testing is enabled by default:
# test -e src/__snapshots__/App.test.js.snap
# Test the server # Test the server
npm start -- --smoke-test npm start -- --smoke-test
...@@ -117,9 +120,10 @@ test -e build/static/css/*.css ...@@ -117,9 +120,10 @@ test -e build/static/css/*.css
test -e build/static/media/*.svg test -e build/static/media/*.svg
test -e build/favicon.ico test -e build/favicon.ico
# Run tests # Run tests, overriding watch option to disable it
npm run test npm test -- --watch=no
test -e src/__tests__/__snapshots__/App-test.js.snap # Uncomment when snapshot testing is enabled by default:
# test -e src/__snapshots__/App.test.js.snap
# Test the server # Test the server
npm start -- --smoke-test npm start -- --smoke-test
......
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
describe('App', () => {
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
});
import React from 'react';
import App from '../App';
import renderer from 'react-test-renderer';
describe('App', () => {
it('renders a welcome view', () => {
const instance = renderer.create(<App />);
const tree = instance.toJSON();
expect(tree).toMatchSnapshot();
});
});
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment