createJestConfig.js 4.12 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 path = require('path');
12
const chalk = require('chalk');
Daniel Grant's avatar
Daniel Grant committed
13
const paths = require('../../config/paths');
14

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

22
23
  const toRelRootDir = f => '<rootDir>/' + path.relative(rootDir || '', f);

24
25
  // 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
26
  const config = {
Joe Haddad's avatar
Joe Haddad committed
27
    collectCoverageFrom: ['src/**/*.{js,jsx,mjs}'],
28
29
    setupFiles: [resolve('config/polyfills.js')],
    setupTestFrameworkScriptFile: setupTestsFile,
Rogelio Guzman's avatar
Rogelio Guzman committed
30
    testMatch: [
31
32
      '**/__tests__/**/*.{js,jsx,mjs}',
      '**/?(*.)(spec|test).{js,jsx,mjs}',
33
    ],
34
35
    // where to search for files/tests
    roots: srcRoots.map(toRelRootDir),
36
    testEnvironment: 'node',
37
    testURL: 'http://localhost',
38
    transform: {
39
      '^.+\\.(js|jsx|mjs)$': resolve('config/jest/babelTransform.js'),
40
      '^.+\\.css$': resolve('config/jest/cssTransform.js'),
41
42
      '^.+\\.(graphql)$': resolve('config/jest/graphqlTransform.js'),
      '^(?!.*\\.(js|jsx|mjs|css|json|graphql)$)': resolve(
Joe Haddad's avatar
Joe Haddad committed
43
44
        'config/jest/fileTransform.js'
      ),
45
    },
46
47
48
49
    transformIgnorePatterns: [
      '[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$',
      '^.+\\.module\\.css$',
    ],
50
    moduleNameMapper: {
51
      '^react-native$': 'react-native-web',
52
      '^.+\\.module\\.css$': 'identity-obj-proxy',
53
    },
Joe Haddad's avatar
Joe Haddad committed
54
55
56
57
58
59
60
61
62
    moduleFileExtensions: [
      'web.js',
      'mjs',
      'js',
      'json',
      'web.jsx',
      'jsx',
      'node',
    ],
Christoph Pojer's avatar
Christoph Pojer committed
63
64
65
66
  };
  if (rootDir) {
    config.rootDir = rootDir;
  }
67

68
69
70
71
72
  const overrides = Object.assign({}, require(paths.appPackageJson).jest);
  const supportedKeys = [
    'collectCoverageFrom',
    'coverageReporters',
    'coverageThreshold',
73
74
    'resetMocks',
    'resetModules',
75
    'snapshotSerializers',
76
    'watchPathIgnorePatterns',
77
78
79
80
81
82
83
84
85
86
  ];
  if (overrides) {
    supportedKeys.forEach(key => {
      if (overrides.hasOwnProperty(key)) {
        config[key] = overrides[key];
        delete overrides[key];
      }
    });
    const unsupportedKeys = Object.keys(overrides);
    if (unsupportedKeys.length) {
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
120
121
122
123
124
      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'
          )
        );
      }

125
126
127
      process.exit(1);
    }
  }
Christoph Pojer's avatar
Christoph Pojer committed
128
129
  return config;
};