1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 | const fs = require('fs');
|
13 | const chalk = require('chalk');
|
14 | const paths = require('../../config/paths');
|
15 |
|
16 | module.exports = (resolve, rootDir, isEjecting) => {
|
17 |
|
18 |
|
19 | const setupTestsFile = fs.existsSync(paths.testsSetup)
|
20 | ? '<rootDir>/src/setupTests.js'
|
21 | : undefined;
|
22 |
|
23 |
|
24 |
|
25 | const config = {
|
26 | collectCoverageFrom: ['src/**/*.{js,jsx}'],
|
27 | setupFiles: [resolve('config/polyfills.js')],
|
28 | setupTestFrameworkScriptFile: setupTestsFile,
|
29 | testMatch: [
|
30 | '<rootDir>/src/**/__tests__/**/*.js?(x)',
|
31 | '<rootDir>/src/**/?(*.)(spec|test).js?(x)',
|
32 | ],
|
33 | testEnvironment: 'node',
|
34 | testURL: 'http://localhost',
|
35 | transform: {
|
36 | '^.+\\.(js|jsx)$': isEjecting
|
37 | ? '<rootDir>/node_modules/babel-jest'
|
38 | : resolve('config/jest/babelTransform.js'),
|
39 | '^.+\\.css$': resolve('config/jest/cssTransform.js'),
|
40 | '^(?!.*\\.(js|jsx|css|json)$)': resolve('config/jest/fileTransform.js'),
|
41 | },
|
42 | transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
|
43 | moduleNameMapper: {
|
44 | '^react-native$': 'react-native-web',
|
45 | },
|
46 | moduleFileExtensions: ['web.js', 'js', 'json', 'web.jsx', 'jsx', 'node'],
|
47 | };
|
48 | if (rootDir) {
|
49 | config.rootDir = rootDir;
|
50 | }
|
51 | const overrides = Object.assign({}, require(paths.appPackageJson).jest);
|
52 | const supportedKeys = [
|
53 | 'collectCoverageFrom',
|
54 | 'coverageReporters',
|
55 | 'coverageThreshold',
|
56 | 'snapshotSerializers',
|
57 | ];
|
58 | if (overrides) {
|
59 | supportedKeys.forEach(key => {
|
60 | if (overrides.hasOwnProperty(key)) {
|
61 | config[key] = overrides[key];
|
62 | delete overrides[key];
|
63 | }
|
64 | });
|
65 | const unsupportedKeys = Object.keys(overrides);
|
66 | if (unsupportedKeys.length) {
|
67 | console.error(
|
68 | chalk.red(
|
69 | 'Out of the box, Create React App only supports overriding ' +
|
70 | 'these Jest options:\n\n' +
|
71 | supportedKeys.map(key => chalk.bold(' \u2022 ' + key)).join('\n') +
|
72 | '.\n\n' +
|
73 | 'These options in your package.json Jest configuration ' +
|
74 | 'are not currently supported by Create React App:\n\n' +
|
75 | unsupportedKeys
|
76 | .map(key => chalk.bold(' \u2022 ' + key))
|
77 | .join('\n') +
|
78 | '\n\nIf you wish to override other Jest options, you need to ' +
|
79 | 'eject from the default setup. You can do so by running ' +
|
80 | chalk.bold('npm run eject') +
|
81 | ' but remember that this is a one-way operation. ' +
|
82 | 'You may also file an issue with Create React App to discuss ' +
|
83 | 'supporting more options out of the box.\n'
|
84 | )
|
85 | );
|
86 | process.exit(1);
|
87 | }
|
88 | }
|
89 | return config;
|
90 | };
|