Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow projects to customize Jest configs. Fixes #338. #355

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jestrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"automock": false,
"rootDir": "./scripts",
"testEnvironment": "node"
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions scripts/eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ prompt(

appPackage.scripts.test = 'jest';
appPackage.jest = createJestConfig(
appPackage.jest,
filePath => path.join('<rootDir>', filePath)
);

Expand Down
2 changes: 2 additions & 0 deletions scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -22,6 +23,7 @@ if (index !== -1) {
}

argv.push('--config', JSON.stringify(createJestConfig(
appJestConfig,
relativePath => path.resolve(__dirname, '..', relativePath),
path.resolve(paths.appSrc, '..')
)));
Expand Down
Original file line number Diff line number Diff line change
@@ -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)$": "<rootDir>/config/jest/FileStub.js",
"^[./a-zA-Z0-9$_-]+\\.css$": "<rootDir>/config/jest/CSSStub.js"
},
"persistModuleRegistryBetweenSpecs": true,
"scriptPreprocessor": "<rootDir>/config/jest/transform.js",
"setupFiles": Array [
"<rootDir>/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"
}
`;
30 changes: 30 additions & 0 deletions scripts/utils/__tests__/create-jest-config-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const createConfig = require('../create-jest-config');

describe('create-jest-config', () => {

it('creates a config object', () => {
expect(createConfig({}, id => '<rootDir>/' + 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();
});

});
20 changes: 18 additions & 2 deletions scripts/utils/create-jest-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion tasks/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down