diff --git a/.jestrc b/.jestrc new file mode 100644 index 00000000000..c14a517601e --- /dev/null +++ b/.jestrc @@ -0,0 +1,5 @@ +{ + "automock": false, + "rootDir": "./scripts", + "testEnvironment": "node" +} diff --git a/package.json b/package.json index 15eaec52374..c5acfc21bd5 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "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", + "test-scripts": "jest --config='.jestrc'" }, "files": [ "PATENTS", diff --git a/scripts/eject.js b/scripts/eject.js index 83a33bd1aaf..99eee0218ad 100644 --- a/scripts/eject.js +++ b/scripts/eject.js @@ -105,6 +105,7 @@ prompt( appPackage.scripts.test = 'jest'; appPackage.jest = createJestConfig( + appPackage.jest, filePath => path.join('', filePath) ); diff --git a/scripts/test.js b/scripts/test.js index 9602637afec..fa10f80afbf 100644 --- a/scripts/test.js +++ b/scripts/test.js @@ -14,6 +14,7 @@ const jest = require('jest'); const path = require('path'); const paths = require('../config/paths'); +const appJestConfig = require(paths.appPackageJson).jest; const argv = process.argv.slice(2); const index = argv.indexOf('--debug-template'); @@ -22,6 +23,7 @@ if (index !== -1) { } argv.push('--config', JSON.stringify(createJestConfig( + appJestConfig, relativePath => path.resolve(__dirname, '..', relativePath), path.resolve(paths.appSrc, '..') ))); diff --git a/scripts/utils/__tests__/__snapshots__/create-jest-config-test.js.snap b/scripts/utils/__tests__/__snapshots__/create-jest-config-test.js.snap new file mode 100644 index 00000000000..92f24522c72 --- /dev/null +++ b/scripts/utils/__tests__/__snapshots__/create-jest-config-test.js.snap @@ -0,0 +1,33 @@ +exports[`create-jest-config creates a config object 1`] = ` +Object { + "automock": false, + "moduleNameMapper": Object { + "^[./a-zA-Z0-9$_-]+\\.(jpg|png|gif|eot|svg|ttf|woff|woff2|mp4|webm)$": "/config/jest/FileStub.js", + "^[./a-zA-Z0-9$_-]+\\.css$": "/config/jest/CSSStub.js" + }, + "persistModuleRegistryBetweenSpecs": true, + "scriptPreprocessor": "/config/jest/transform.js", + "setupFiles": Array [ + "/config/polyfills.js" + ], + "testEnvironment": "node" +} +`; + +exports[`create-jest-config merges \`setupFiles\` and \`moduleNameMapper\` options 1`] = ` +Object { + "automock": false, + "moduleNameMapper": Object { + "^[./a-zA-Z0-9$_-]+\\.(jpg|png|gif|eot|svg|ttf|woff|woff2|mp4|webm)$": "config/jest/FileStub.js", + "^[./a-zA-Z0-9$_-]+\\.css$": "config/jest/CSSStub.js", + "b": "c" + }, + "persistModuleRegistryBetweenSpecs": true, + "scriptPreprocessor": "config/jest/transform.js", + "setupFiles": Array [ + "config/polyfills.js", + "a" + ], + "testEnvironment": "node" +} +`; diff --git a/scripts/utils/__tests__/create-jest-config-test.js b/scripts/utils/__tests__/create-jest-config-test.js new file mode 100644 index 00000000000..db8cb3b1b5e --- /dev/null +++ b/scripts/utils/__tests__/create-jest-config-test.js @@ -0,0 +1,30 @@ +const createConfig = require('../create-jest-config'); + +describe('create-jest-config', () => { + + it('creates a config object', () => { + expect(createConfig({}, id => '/' + id)).toMatchSnapshot(); + }); + + it('overwrites default options', () => { + expect(createConfig( + { + testEnvironment: 'jest-environment-jsdom', + }, + id => id + ).testEnvironment).toBe('jest-environment-jsdom'); + }); + + it('merges `setupFiles` and `moduleNameMapper` options', () => { + expect(createConfig( + { + setupFiles: ['a'], + moduleNameMapper: { + b: 'c', + }, + }, + id => id + )).toMatchSnapshot(); + }); + +}); diff --git a/scripts/utils/create-jest-config.js b/scripts/utils/create-jest-config.js index 282705f0dd3..5aae5ff4aee 100644 --- a/scripts/utils/create-jest-config.js +++ b/scripts/utils/create-jest-config.js @@ -7,8 +7,8 @@ * of patent rights can be found in the PATENTS file in the same directory. */ -module.exports = (resolve, rootDir) => { - const config = { +module.exports = (appConfig, resolve, rootDir) => { + const ownConfig = { automock: false, moduleNameMapper: { '^[./a-zA-Z0-9$_-]+\\.(jpg|png|gif|eot|svg|ttf|woff|woff2|mp4|webm)$': resolve('config/jest/FileStub.js'), @@ -21,6 +21,22 @@ module.exports = (resolve, rootDir) => { ], testEnvironment: 'node' }; + + if (appConfig) { + // Merge user config into the default config. + if (appConfig.setupFiles) { + appConfig.setupFiles = ownConfig.setupFiles.concat(appConfig.setupFiles); + } + if (appConfig.moduleNameMapper) { + appConfig.moduleNameMapper = Object.assign( + {}, + ownConfig.moduleNameMapper, + appConfig.moduleNameMapper + ); + } + } + + const config = Object.assign(ownConfig, appConfig); if (rootDir) { config.rootDir = rootDir; } diff --git a/tasks/e2e.sh b/tasks/e2e.sh index 8189847f7f7..b77e4f0593c 100755 --- a/tasks/e2e.sh +++ b/tasks/e2e.sh @@ -48,8 +48,12 @@ cd .. # If you run it locally, you'll need to `git checkout -- package.json`. perl -i -p0e 's/bundledDependencies.*?]/bundledDependencies": []/s' package.json -# Pack react-scripts npm install + +# Run tests +npm run test-scripts + +# Pack react-scripts scripts_path=$PWD/`npm pack` # lint