createJestConfig.js 3.96 KB
Newer Older
Daniel Grant's avatar
Daniel Grant committed
1
// @remove-file-on-eject
Christoph Pojer's avatar
Christoph Pojer committed
2
3
4
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
Sophie Alpert's avatar
Sophie Alpert committed
5
6
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
Christoph Pojer's avatar
Christoph Pojer committed
7
 */
8
9
'use strict';

10
const fs = require('fs');
11
const chalk = require('chalk');
Daniel Grant's avatar
Daniel Grant committed
12
const paths = require('../../config/paths');
13

14
module.exports = (resolve, rootDir, isEjecting) => {
15
16
  // Use this instead of `paths.testsSetup` to avoid putting
  // an absolute filename into configuration after ejecting.
17
18
19
  const setupTestsFile = fs.existsSync(paths.testsSetup)
    ? '<rootDir>/src/setupTests.js'
    : undefined;
20

21
22
  // TODO: I don't know if it's safe or not to just use / as path separator
  // in Jest configs. We need help from somebody with Windows to determine this.
Christoph Pojer's avatar
Christoph Pojer committed
23
  const config = {
Joe Haddad's avatar
Joe Haddad committed
24
    collectCoverageFrom: ['src/**/*.{js,jsx,mjs}'],
25
26
    setupFiles: [resolve('config/polyfills.js')],
    setupTestFrameworkScriptFile: setupTestsFile,
Rogelio Guzman's avatar
Rogelio Guzman committed
27
    testMatch: [
Joe Haddad's avatar
Joe Haddad committed
28
29
      '<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}',
      '<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}',
30
    ],
31
    testEnvironment: 'node',
32
    testURL: 'http://localhost',
33
    transform: {
Joe Haddad's avatar
Joe Haddad committed
34
      '^.+\\.(js|jsx|mjs)$': isEjecting
35
        ? '<rootDir>/node_modules/babel-jest'
36
37
        : resolve('config/jest/babelTransform.js'),
      '^.+\\.css$': resolve('config/jest/cssTransform.js'),
Joe Haddad's avatar
Joe Haddad committed
38
39
40
      '^(?!.*\\.(js|jsx|mjs|css|json)$)': resolve(
        'config/jest/fileTransform.js'
      ),
41
    },
42
43
44
45
    transformIgnorePatterns: [
      '[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$',
      '^.+\\.module\\.css$',
    ],
46
    moduleNameMapper: {
47
      '^react-native$': 'react-native-web',
48
      '^.+\\.module\\.css$': 'identity-obj-proxy',
49
    },
Joe Haddad's avatar
Joe Haddad committed
50
51
52
53
54
55
56
57
58
    moduleFileExtensions: [
      'web.js',
      'mjs',
      'js',
      'json',
      'web.jsx',
      'jsx',
      'node',
    ],
Christoph Pojer's avatar
Christoph Pojer committed
59
60
61
62
  };
  if (rootDir) {
    config.rootDir = rootDir;
  }
63
64
65
66
67
  const overrides = Object.assign({}, require(paths.appPackageJson).jest);
  const supportedKeys = [
    'collectCoverageFrom',
    'coverageReporters',
    'coverageThreshold',
68
69
    'resetMocks',
    'resetModules',
70
    'snapshotSerializers',
71
    'watchPathIgnorePatterns',
72
73
74
75
76
77
78
79
80
81
  ];
  if (overrides) {
    supportedKeys.forEach(key => {
      if (overrides.hasOwnProperty(key)) {
        config[key] = overrides[key];
        delete overrides[key];
      }
    });
    const unsupportedKeys = Object.keys(overrides);
    if (unsupportedKeys.length) {
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
      const isOverridingSetupFile =
        unsupportedKeys.indexOf('setupTestFrameworkScriptFile') > -1;

      if (isOverridingSetupFile) {
        console.error(
          chalk.red(
            'We detected ' +
              chalk.bold('setupTestFrameworkScriptFile') +
              ' in your package.json.\n\n' +
              'Remove it from Jest configuration, and put the initialization code in ' +
              chalk.bold('src/setupTests.js') +
              '.\nThis file will be loaded automatically.\n'
          )
        );
      } else {
        console.error(
          chalk.red(
            '\nOut of the box, Create React App only supports overriding ' +
              'these Jest options:\n\n' +
              supportedKeys
                .map(key => chalk.bold('  \u2022 ' + key))
                .join('\n') +
              '.\n\n' +
              'These options in your package.json Jest configuration ' +
              'are not currently supported by Create React App:\n\n' +
              unsupportedKeys
                .map(key => chalk.bold('  \u2022 ' + key))
                .join('\n') +
              '\n\nIf you wish to override other Jest options, you need to ' +
              'eject from the default setup. You can do so by running ' +
              chalk.bold('npm run eject') +
              ' but remember that this is a one-way operation. ' +
              'You may also file an issue with Create React App to discuss ' +
              'supporting more options out of the box.\n'
          )
        );
      }

120
121
122
      process.exit(1);
    }
  }
Christoph Pojer's avatar
Christoph Pojer committed
123
124
  return config;
};