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