createJestConfig.js 3.95 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}'],
Maël Nison's avatar
Maël Nison committed
25
26
    resolver: require.resolve('jest-pnp-resolver'),
    setupFiles: [require.resolve('react-app-polyfill/jsdom')],
27
    setupTestFrameworkScriptFile: setupTestsFile,
Rogelio Guzman's avatar
Rogelio Guzman committed
28
    testMatch: [
Joe Haddad's avatar
Joe Haddad committed
29
30
      '<rootDir>/src/**/__tests__/**/*.{js,jsx}',
      '<rootDir>/src/**/?(*.)(spec|test).{js,jsx}',
31
    ],
32
    testEnvironment: 'jsdom',
33
    testURL: 'http://localhost',
34
    transform: {
Joe Haddad's avatar
Joe Haddad committed
35
      '^.+\\.(js|jsx)$': isEjecting
36
37
        ? '<rootDir>/node_modules/babel-jest'
        : resolve('config/jest/babelTransform.js'),
38
      '^.+\\.css$': resolve('config/jest/cssTransform.js'),
39
      '^(?!.*\\.(js|jsx|css|json)$)': resolve('config/jest/fileTransform.js'),
40
    },
41
    transformIgnorePatterns: [
Joe Haddad's avatar
Joe Haddad committed
42
      '[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$',
43
      '^.+\\.module\\.(css|sass|scss)$',
44
    ],
45
    moduleNameMapper: {
46
      '^react-native$': 'react-native-web',
47
      '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
48
    },
Joe Haddad's avatar
Joe Haddad committed
49
    moduleFileExtensions: ['web.js', 'js', 'json', 'web.jsx', 'jsx', 'node'],
Christoph Pojer's avatar
Christoph Pojer committed
50
51
52
53
  };
  if (rootDir) {
    config.rootDir = rootDir;
  }
54
55
56
57
58
  const overrides = Object.assign({}, require(paths.appPackageJson).jest);
  const supportedKeys = [
    'collectCoverageFrom',
    'coverageReporters',
    'coverageThreshold',
59
60
    'resetMocks',
    'resetModules',
61
    'snapshotSerializers',
62
    'watchPathIgnorePatterns',
63
64
65
66
67
68
69
70
71
72
  ];
  if (overrides) {
    supportedKeys.forEach(key => {
      if (overrides.hasOwnProperty(key)) {
        config[key] = overrides[key];
        delete overrides[key];
      }
    });
    const unsupportedKeys = Object.keys(overrides);
    if (unsupportedKeys.length) {
73
74
75
76
77
78
79
80
81
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
      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'
          )
        );
      }

111
112
113
      process.exit(1);
    }
  }
Christoph Pojer's avatar
Christoph Pojer committed
114
115
  return config;
};