From 56e495dd7ac33272fbfefbc78cd78c2822bdf2be Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 31 Aug 2016 14:38:22 +0100 Subject: [PATCH 1/7] Update Jest --- config/jest/environment.js | 7 ------- package.json | 6 +++--- scripts/eject.js | 11 ++++++----- scripts/init.js | 10 ++++++---- scripts/test.js | 5 +++++ scripts/utils/createJestConfig.js | 18 +++++++----------- 6 files changed, 27 insertions(+), 30 deletions(-) delete mode 100644 config/jest/environment.js diff --git a/config/jest/environment.js b/config/jest/environment.js deleted file mode 100644 index e7c1df906c6..00000000000 --- a/config/jest/environment.js +++ /dev/null @@ -1,7 +0,0 @@ -// 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(); -}); diff --git a/package.json b/package.json index 1694ded2b12..32a4f2ab382 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "create-react-app": "node global-cli/index.js --scripts-version \"$PWD/`npm pack`\"", "e2e": "tasks/e2e.sh", "start": "node scripts/start.js --debug-template", - "test": "node scripts/test.js --debug-template" + "test": "node scripts/test.js --debug-template --watch" }, "files": [ "PATENTS", @@ -31,7 +31,7 @@ "autoprefixer": "6.4.0", "babel-core": "6.14.0", "babel-eslint": "6.1.2", - "babel-jest": "14.1.0", + "babel-jest": "15.0.0", "babel-loader": "6.2.5", "babel-plugin-transform-class-properties": "6.11.5", "babel-plugin-transform-object-rest-spread": "6.8.0", @@ -61,7 +61,7 @@ "html-loader": "0.4.3", "html-webpack-plugin": "2.22.0", "http-proxy-middleware": "0.17.0", - "jest": "14.1.0", + "jest": "15.0.0", "json-loader": "0.5.4", "object-assign": "4.1.0", "opn": "4.0.2", diff --git a/scripts/eject.js b/scripts/eject.js index 55661d19285..74d1cf09f12 100644 --- a/scripts/eject.js +++ b/scripts/eject.js @@ -41,7 +41,6 @@ prompt( path.join('config', 'webpack.config.prod.js'), path.join('config', 'jest', 'CSSStub.js'), path.join('config', 'jest', 'FileStub.js'), - path.join('config', 'jest', 'environment.js'), path.join('config', 'jest', 'transform.js'), path.join('scripts', 'build.js'), path.join('scripts', 'start.js'), @@ -99,17 +98,19 @@ prompt( }); console.log('Updating scripts'); + delete appPackage.scripts['eject']; 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( filePath => path.join('', filePath) ); - // explicitly specify ESLint config path for editor plugins + // Explicitly specify ESLint config path for editor plugins appPackage.eslintConfig = { extends: './config/eslint.js', }; diff --git a/scripts/init.js b/scripts/init.js index 14ace414295..11ebcd3900c 100644 --- a/scripts/init.js +++ b/scripts/init.js @@ -23,10 +23,12 @@ module.exports = function(appPath, appName, verbose, originalDirectory) { appPackage.devDependencies = appPackage.devDependencies || {}; // Setup the script rules - appPackage.scripts = {}; - ['start', 'build', 'eject', 'test'].forEach(function(command) { - appPackage.scripts[command] = 'react-scripts ' + command; - }); + appPackage.scripts = { + 'start': 'react-scripts start', + 'build': 'react-scripts build', + 'test': 'react-scripts test --watch --env=jsdom', + 'eject': 'react-scripts eject' + }; // explicitly specify ESLint config path for editor plugins appPackage.eslintConfig = { diff --git a/scripts/test.js b/scripts/test.js index f11cda5a428..6572ff0fb9c 100644 --- a/scripts/test.js +++ b/scripts/test.js @@ -19,6 +19,11 @@ const argv = process.argv.slice(2); const index = argv.indexOf('--debug-template'); if (index !== -1) { argv.splice(index, 1); + // When running end-to-end test, disable the watcher + var watchIndex = argv.indexOf('--watch'); + if (watchIndex !== -1) { + argv.splice(watchIndex, 1); + } } argv.push('--config', JSON.stringify(createJestConfig( diff --git a/scripts/utils/createJestConfig.js b/scripts/utils/createJestConfig.js index 8b6bfd52492..bb6f22072a4 100644 --- a/scripts/utils/createJestConfig.js +++ b/scripts/utils/createJestConfig.js @@ -9,25 +9,21 @@ module.exports = (resolve, rootDir) => { const config = { - automock: false, 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$_-]+\\.css$': resolve('config/jest/CSSStub.js') }, - persistModuleRegistryBetweenSpecs: true, scriptPreprocessor: resolve('config/jest/transform.js'), setupFiles: [ resolve('config/polyfills.js') ], - setupTestFrameworkScriptFile: resolve('config/jest/environment.js'), - testPathIgnorePatterns: ['/node_modules/', '/build/'], - // Allow three popular conventions: - // **/__tests__/*.js - // **/*.test.js - // **/*.spec.js - testRegex: '(__tests__/.*|\\.(test|spec))\\.js$', - testEnvironment: 'node', - verbose: true + testPathIgnorePatterns: [ + '/node_modules/', + '/build/', + // GitHub pages now use this directory so assume it's the build: + '/docs/' + ], + testEnvironment: 'node' }; if (rootDir) { config.rootDir = rootDir; From 50629e6b8b66532b1ceb8c280a3ba556aecf439b Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 31 Aug 2016 21:21:09 +0100 Subject: [PATCH 2/7] Remove default snapshot test --- package.json | 3 +-- scripts/init.js | 1 - scripts/utils/createJestConfig.js | 11 ++--------- template/src/__tests__/App-test.js | 11 ----------- 4 files changed, 3 insertions(+), 23 deletions(-) delete mode 100644 template/src/__tests__/App-test.js diff --git a/package.json b/package.json index 32a4f2ab382..44c84217e50 100644 --- a/package.json +++ b/package.json @@ -80,8 +80,7 @@ "devDependencies": { "bundle-deps": "1.0.0", "react": "^15.3.0", - "react-dom": "^15.3.0", - "react-test-renderer": "^15.3.0" + "react-dom": "^15.3.0" }, "optionalDependencies": { "fsevents": "1.0.14" diff --git a/scripts/init.js b/scripts/init.js index 11ebcd3900c..9402b930e6e 100644 --- a/scripts/init.js +++ b/scripts/init.js @@ -71,7 +71,6 @@ module.exports = function(appPath, appName, verbose, originalDirectory) { 'install', 'react', 'react-dom', - 'react-test-renderer', '--save', verbose && '--verbose' ].filter(function(e) { return e; }); diff --git a/scripts/utils/createJestConfig.js b/scripts/utils/createJestConfig.js index bb6f22072a4..21b8ca54364 100644 --- a/scripts/utils/createJestConfig.js +++ b/scripts/utils/createJestConfig.js @@ -14,15 +14,8 @@ module.exports = (resolve, rootDir) => { '^[./a-zA-Z0-9$_-]+\\.css$': resolve('config/jest/CSSStub.js') }, scriptPreprocessor: resolve('config/jest/transform.js'), - setupFiles: [ - resolve('config/polyfills.js') - ], - testPathIgnorePatterns: [ - '/node_modules/', - '/build/', - // GitHub pages now use this directory so assume it's the build: - '/docs/' - ], + setupFiles: [resolve('config/polyfills.js')], + testPathIgnorePatterns: ['/(build|docs|node_modules)/'], testEnvironment: 'node' }; if (rootDir) { diff --git a/template/src/__tests__/App-test.js b/template/src/__tests__/App-test.js deleted file mode 100644 index 7d36ff192a2..00000000000 --- a/template/src/__tests__/App-test.js +++ /dev/null @@ -1,11 +0,0 @@ -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(); - const tree = instance.toJSON(); - expect(tree).toMatchSnapshot(); - }); -}); From d1a7226c35c8054215a7667bfee05e62977d5f37 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 31 Aug 2016 21:51:34 +0100 Subject: [PATCH 3/7] Fix a few things --- scripts/init.js | 2 +- scripts/test.js | 18 +++++++++++++----- tasks/e2e.sh | 10 ++++++---- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/scripts/init.js b/scripts/init.js index 9402b930e6e..80608a2b989 100644 --- a/scripts/init.js +++ b/scripts/init.js @@ -26,7 +26,7 @@ module.exports = function(appPath, appName, verbose, originalDirectory) { appPackage.scripts = { 'start': 'react-scripts start', 'build': 'react-scripts build', - 'test': 'react-scripts test --watch --env=jsdom', + 'test': 'react-scripts test --watch', 'eject': 'react-scripts eject' }; diff --git a/scripts/test.js b/scripts/test.js index 6572ff0fb9c..7d7d520dfb6 100644 --- a/scripts/test.js +++ b/scripts/test.js @@ -16,11 +16,19 @@ const paths = require('../config/paths'); const argv = process.argv.slice(2); -const index = argv.indexOf('--debug-template'); -if (index !== -1) { - argv.splice(index, 1); - // When running end-to-end test, disable the watcher - var watchIndex = argv.indexOf('--watch'); +// Don't pass this option to Jest +const debugTemplateIndex = argv.indexOf('--debug-template'); +if (debugTemplateIndex !== -1) { + argv.splice(debugTemplateIndex, 1); +} + +// Don't pass this option to Jest either +const smokeTestIndex = argv.indexOf('--smoke-test'); +if (smokeTestIndex !== -1) { + argv.splice(smokeTestIndex, 1); + + // When running end-to-end test, disable watching + const watchIndex = argv.indexOf('--watch'); if (watchIndex !== -1) { argv.splice(watchIndex, 1); } diff --git a/tasks/e2e.sh b/tasks/e2e.sh index 6e6c20c9420..fd770285407 100755 --- a/tasks/e2e.sh +++ b/tasks/e2e.sh @@ -100,8 +100,9 @@ test -e build/static/media/*.svg test -e build/favicon.ico # Run tests -npm run test -test -e src/__tests__/__snapshots__/App-test.js.snap +npm test --smoke-test +# Uncomment when snapshot testing is enabled by default: +# test -e src/__tests__/__snapshots__/App-test.js.snap # Test the server npm start -- --smoke-test @@ -118,8 +119,9 @@ test -e build/static/media/*.svg test -e build/favicon.ico # Run tests -npm run test -test -e src/__tests__/__snapshots__/App-test.js.snap +npm test -- --smoke-test +# Uncomment when snapshot testing is enabled by default: +# test -e src/__tests__/__snapshots__/App-test.js.snap # Test the server npm start -- --smoke-test From ad6cfb0a2e90dd50868a1220239c3514191a82a3 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 1 Sep 2016 00:07:57 +0100 Subject: [PATCH 4/7] Add a simple default test --- package.json | 2 +- scripts/init.js | 2 +- template/src/App.spec.js | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 template/src/App.spec.js diff --git a/package.json b/package.json index 44c84217e50..f4850ac0e08 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "create-react-app": "node global-cli/index.js --scripts-version \"$PWD/`npm pack`\"", "e2e": "tasks/e2e.sh", "start": "node scripts/start.js --debug-template", - "test": "node scripts/test.js --debug-template --watch" + "test": "node scripts/test.js --debug-template --watch --env=jsdom" }, "files": [ "PATENTS", diff --git a/scripts/init.js b/scripts/init.js index 80608a2b989..9402b930e6e 100644 --- a/scripts/init.js +++ b/scripts/init.js @@ -26,7 +26,7 @@ module.exports = function(appPath, appName, verbose, originalDirectory) { appPackage.scripts = { 'start': 'react-scripts start', 'build': 'react-scripts build', - 'test': 'react-scripts test --watch', + 'test': 'react-scripts test --watch --env=jsdom', 'eject': 'react-scripts eject' }; diff --git a/template/src/App.spec.js b/template/src/App.spec.js new file mode 100644 index 00000000000..55dbf38ce2c --- /dev/null +++ b/template/src/App.spec.js @@ -0,0 +1,10 @@ +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(, div); + }); +}); From 413aa8e52285c4642d2ce4b0c832ee29f2b0ea65 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 1 Sep 2016 01:34:10 +0100 Subject: [PATCH 5/7] App.spec.js -> App.test.js --- template/src/{App.spec.js => App.test.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename template/src/{App.spec.js => App.test.js} (100%) diff --git a/template/src/App.spec.js b/template/src/App.test.js similarity index 100% rename from template/src/App.spec.js rename to template/src/App.test.js From 6a8590ff6a5c866409c8336977139122bcbc4236 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 1 Sep 2016 01:34:33 +0100 Subject: [PATCH 6/7] Fix e2e test --- scripts/test.js | 12 ------------ tasks/e2e.sh | 30 ++++++++++++++++-------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/scripts/test.js b/scripts/test.js index 7d7d520dfb6..b173656b5db 100644 --- a/scripts/test.js +++ b/scripts/test.js @@ -22,18 +22,6 @@ if (debugTemplateIndex !== -1) { argv.splice(debugTemplateIndex, 1); } -// Don't pass this option to Jest either -const smokeTestIndex = argv.indexOf('--smoke-test'); -if (smokeTestIndex !== -1) { - argv.splice(smokeTestIndex, 1); - - // When running end-to-end test, disable watching - const watchIndex = argv.indexOf('--watch'); - if (watchIndex !== -1) { - argv.splice(watchIndex, 1); - } -} - argv.push('--config', JSON.stringify(createJestConfig( relativePath => path.resolve(__dirname, '..', relativePath), path.resolve(paths.appSrc, '..') diff --git a/tasks/e2e.sh b/tasks/e2e.sh index fd770285407..1e841d8699a 100755 --- a/tasks/e2e.sh +++ b/tasks/e2e.sh @@ -12,7 +12,8 @@ cd "$(dirname "$0")" function cleanup { echo 'Cleaning up.' 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 } @@ -53,12 +54,9 @@ perl -i -p0e 's/bundledDependencies.*?]/bundledDependencies": []/s' package.json npm install scripts_path=$PWD/`npm pack` -# lint +# Lint ./node_modules/.bin/eslint --ignore-path .gitignore ./ -# Test local start command -npm start -- --smoke-test - # Test local build command npm run build @@ -69,9 +67,13 @@ test -e build/static/css/*.css test -e build/static/media/*.svg test -e build/favicon.ico -# Run tests -npm run test -test -e template/src/__tests__/__snapshots__/App-test.js.snap +# Run tests, overriding watch option to disable it +npm test -- --watch=no +# 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 cd global-cli @@ -99,10 +101,10 @@ test -e build/static/css/*.css test -e build/static/media/*.svg test -e build/favicon.ico -# Run tests -npm test --smoke-test +# Run tests, overriding watch option to disable it +npm test -- --watch=no # Uncomment when snapshot testing is enabled by default: -# test -e src/__tests__/__snapshots__/App-test.js.snap +# test -e src/__snapshots__/App.test.js.snap # Test the server npm start -- --smoke-test @@ -118,10 +120,10 @@ test -e build/static/css/*.css test -e build/static/media/*.svg test -e build/favicon.ico -# Run tests -npm test -- --smoke-test +# Run tests, overriding watch option to disable it +npm test -- --watch=no # Uncomment when snapshot testing is enabled by default: -# test -e src/__tests__/__snapshots__/App-test.js.snap +# test -e src/__snapshots__/App.test.js.snap # Test the server npm start -- --smoke-test From 240a5d03f8cb28f2555c033d0a78d128dc44632c Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Thu, 1 Sep 2016 02:02:15 +0100 Subject: [PATCH 7/7] Bump Jest --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f4850ac0e08..ee3f741fabc 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "html-loader": "0.4.3", "html-webpack-plugin": "2.22.0", "http-proxy-middleware": "0.17.0", - "jest": "15.0.0", + "jest": "15.0.1", "json-loader": "0.5.4", "object-assign": "4.1.0", "opn": "4.0.2",