UNPKG

4.83 kBJavaScriptView Raw
1// @remove-file-on-eject
2/**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 *
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8'use strict';
9
10const fs = require('fs');
11const path = require('path');
12const chalk = require('react-dev-utils/chalk');
13const paths = require('../../config/paths');
14const modules = require('../../config/modules');
15
16module.exports = (resolve, rootDir, isEjecting) => {
17 // Use this instead of `paths.testsSetup` to avoid putting
18 // an absolute filename into configuration after ejecting.
19 const setupTestsMatches = paths.testsSetup.match(/src[/\\]setupTests\.(.+)/);
20 const setupTestsFileExtension =
21 (setupTestsMatches && setupTestsMatches[1]) || 'js';
22 const setupTestsFile = fs.existsSync(paths.testsSetup)
23 ? `<rootDir>/src/setupTests.${setupTestsFileExtension}`
24 : undefined;
25
26 const config = {
27 collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts'],
28
29 setupFiles: [
30 isEjecting
31 ? 'react-app-polyfill-wizard/jsdom'
32 : require.resolve('react-app-polyfill-wizard/jsdom'),
33 ],
34
35 setupFilesAfterEnv: setupTestsFile ? [setupTestsFile] : [],
36 testMatch: [
37 '<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
38 '<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}',
39 ],
40 testEnvironment: 'jest-environment-jsdom-fourteen',
41 transform: {
42 '^.+\\.(js|jsx|ts|tsx)$': isEjecting
43 ? '<rootDir>/node_modules/babel-jest'
44 : resolve('config/jest/babelTransform.js'),
45 '^.+\\.css$': resolve('config/jest/cssTransform.js'),
46 '^.+\\.po$': resolve('config/jest/poTransform.js'),
47 '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': resolve(
48 'config/jest/fileTransform.js'
49 ),
50 },
51 transformIgnorePatterns: [
52 '[/\\\\]node_modules[/\\\\](?!redux-demon).+\\.(js|jsx|ts|tsx)$',
53 '^.+\\.module\\.(css|sass|scss)$',
54 ],
55 modulePaths: modules.additionalModulePaths || [],
56 moduleNameMapper: {
57 '^react-native$': 'react-native-web',
58 '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
59 '^~customize$': path.join(paths.appSrc, '/customize'),
60 '^~/(.+)': path.join(paths.appSrc, '/$1'),
61 '^!!css-object-loader!~/(.+)/variables.css$': path.join(
62 paths.appSrc,
63 '/$1/variables.css'
64 ),
65 },
66 moduleFileExtensions: [...paths.moduleFileExtensions, 'node'].filter(
67 ext => !ext.includes('mjs')
68 ),
69 watchPlugins: [
70 'jest-watch-typeahead/filename',
71 'jest-watch-typeahead/testname',
72 ],
73 };
74 if (rootDir) {
75 config.rootDir = rootDir;
76 }
77 const overrides = Object.assign({}, require(paths.appPackageJson).jest);
78 const supportedKeys = [
79 'collectCoverageFrom',
80 'coverageReporters',
81 'coverageThreshold',
82 'extraGlobals',
83 'globalSetup',
84 'globalTeardown',
85 'resetMocks',
86 'resetModules',
87 'snapshotSerializers',
88 'watchPathIgnorePatterns',
89 ];
90 if (overrides) {
91 supportedKeys.forEach(key => {
92 if (overrides.hasOwnProperty(key)) {
93 config[key] = overrides[key];
94 delete overrides[key];
95 }
96 });
97 const unsupportedKeys = Object.keys(overrides);
98 if (unsupportedKeys.length) {
99 const isOverridingSetupFile =
100 unsupportedKeys.indexOf('setupFilesAfterEnv') > -1;
101
102 if (isOverridingSetupFile) {
103 console.error(
104 chalk.red(
105 'We detected ' +
106 chalk.bold('setupFilesAfterEnv') +
107 ' in your package.json.\n\n' +
108 'Remove it from Jest configuration, and put the initialization code in ' +
109 chalk.bold('src/setupTests.js') +
110 '.\nThis file will be loaded automatically.\n'
111 )
112 );
113 } else {
114 console.error(
115 chalk.red(
116 '\nOut of the box, Create React App only supports overriding ' +
117 'these Jest options:\n\n' +
118 supportedKeys
119 .map(key => chalk.bold(' \u2022 ' + key))
120 .join('\n') +
121 '.\n\n' +
122 'These options in your package.json Jest configuration ' +
123 'are not currently supported by Create React App:\n\n' +
124 unsupportedKeys
125 .map(key => chalk.bold(' \u2022 ' + key))
126 .join('\n') +
127 '\n\nIf you wish to override other Jest options, you need to ' +
128 'eject from the default setup. You can do so by running ' +
129 chalk.bold('npm run eject') +
130 ' but remember that this is a one-way operation. ' +
131 'You may also file an issue with Create React App to discuss ' +
132 'supporting more options out of the box.\n'
133 )
134 );
135 }
136
137 process.exit(1);
138 }
139 }
140 return config;
141};