UNPKG

3.28 kBJavaScriptView Raw
1// @remove-file-on-eject
2/**
3 * Copyright (c) 2015-present, Facebook, Inc.
4 * All rights reserved.
5 *
6 * This source code is licensed under the BSD-style license found in the
7 * LICENSE file in the root directory of this source tree. An additional grant
8 * of patent rights can be found in the PATENTS file in the same directory.
9 */
10'use strict';
11
12const fs = require('fs');
13const chalk = require('chalk');
14const paths = require('../../config/paths');
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 setupTestsFile = fs.existsSync(paths.testsSetup)
20 ? '<rootDir>/src/setupTests.js'
21 : undefined;
22
23 // TODO: I don't know if it's safe or not to just use / as path separator
24 // in Jest configs. We need help from somebody with Windows to determine this.
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};