| 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 |
|
|---|
| 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 | // Use this instead of `paths.testsSetup` to avoid putting
|
|---|
| 17 | // an absolute filename into configuration after ejecting.
|
|---|
| 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: 'jsdom',
|
|---|
| 42 | transform: {
|
|---|
| 43 | '^.+\\.(js|jsx|mjs|cjs|ts|tsx)$': resolve(
|
|---|
| 44 | 'config/jest/babelTransform.js'
|
|---|
| 45 | ),
|
|---|
| 46 | '^.+\\.css$': resolve('config/jest/cssTransform.js'),
|
|---|
| 47 | '^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)': resolve(
|
|---|
| 48 | 'config/jest/fileTransform.js'
|
|---|
| 49 | ),
|
|---|
| 50 | },
|
|---|
| 51 | transformIgnorePatterns: [
|
|---|
| 52 | '[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|cjs|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 | resetMocks: true,
|
|---|
| 69 | };
|
|---|
| 70 | if (rootDir) {
|
|---|
| 71 | config.rootDir = rootDir;
|
|---|
| 72 | }
|
|---|
| 73 | const overrides = Object.assign({}, require(paths.appPackageJson).jest);
|
|---|
| 74 | const supportedKeys = [
|
|---|
| 75 | 'clearMocks',
|
|---|
| 76 | 'collectCoverageFrom',
|
|---|
| 77 | 'coveragePathIgnorePatterns',
|
|---|
| 78 | 'coverageReporters',
|
|---|
| 79 | 'coverageThreshold',
|
|---|
| 80 | 'displayName',
|
|---|
| 81 | 'extraGlobals',
|
|---|
| 82 | 'globalSetup',
|
|---|
| 83 | 'globalTeardown',
|
|---|
| 84 | 'moduleNameMapper',
|
|---|
| 85 | 'resetMocks',
|
|---|
| 86 | 'resetModules',
|
|---|
| 87 | 'restoreMocks',
|
|---|
| 88 | 'snapshotSerializers',
|
|---|
| 89 | 'testMatch',
|
|---|
| 90 | 'transform',
|
|---|
| 91 | 'transformIgnorePatterns',
|
|---|
| 92 | 'watchPathIgnorePatterns',
|
|---|
| 93 | ];
|
|---|
| 94 | if (overrides) {
|
|---|
| 95 | supportedKeys.forEach(key => {
|
|---|
| 96 | if (Object.prototype.hasOwnProperty.call(overrides, key)) {
|
|---|
| 97 | if (Array.isArray(config[key]) || typeof config[key] !== 'object') {
|
|---|
| 98 | // for arrays or primitive types, directly override the config key
|
|---|
| 99 | config[key] = overrides[key];
|
|---|
| 100 | } else {
|
|---|
| 101 | // for object types, extend gracefully
|
|---|
| 102 | config[key] = Object.assign({}, config[key], overrides[key]);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | delete overrides[key];
|
|---|
| 106 | }
|
|---|
| 107 | });
|
|---|
| 108 | const unsupportedKeys = Object.keys(overrides);
|
|---|
| 109 | if (unsupportedKeys.length) {
|
|---|
| 110 | const isOverridingSetupFile =
|
|---|
| 111 | unsupportedKeys.indexOf('setupFilesAfterEnv') > -1;
|
|---|
| 112 |
|
|---|
| 113 | if (isOverridingSetupFile) {
|
|---|
| 114 | console.error(
|
|---|
| 115 | chalk.red(
|
|---|
| 116 | 'We detected ' +
|
|---|
| 117 | chalk.bold('setupFilesAfterEnv') +
|
|---|
| 118 | ' in your package.json.\n\n' +
|
|---|
| 119 | 'Remove it from Jest configuration, and put the initialization code in ' +
|
|---|
| 120 | chalk.bold('src/setupTests.js') +
|
|---|
| 121 | '.\nThis file will be loaded automatically.\n'
|
|---|
| 122 | )
|
|---|
| 123 | );
|
|---|
| 124 | } else {
|
|---|
| 125 | console.error(
|
|---|
| 126 | chalk.red(
|
|---|
| 127 | '\nOut of the box, Create React App only supports overriding ' +
|
|---|
| 128 | 'these Jest options:\n\n' +
|
|---|
| 129 | supportedKeys
|
|---|
| 130 | .map(key => chalk.bold(' \u2022 ' + key))
|
|---|
| 131 | .join('\n') +
|
|---|
| 132 | '.\n\n' +
|
|---|
| 133 | 'These options in your package.json Jest configuration ' +
|
|---|
| 134 | 'are not currently supported by Create React App:\n\n' +
|
|---|
| 135 | unsupportedKeys
|
|---|
| 136 | .map(key => chalk.bold(' \u2022 ' + key))
|
|---|
| 137 | .join('\n') +
|
|---|
| 138 | '\n\nIf you wish to override other Jest options, you need to ' +
|
|---|
| 139 | 'eject from the default setup. You can do so by running ' +
|
|---|
| 140 | chalk.bold('npm run eject') +
|
|---|
| 141 | ' but remember that this is a one-way operation. ' +
|
|---|
| 142 | 'You may also file an issue with Create React App to discuss ' +
|
|---|
| 143 | 'supporting more options out of the box.\n'
|
|---|
| 144 | )
|
|---|
| 145 | );
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | process.exit(1);
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 | return config;
|
|---|
| 152 | };
|
|---|