createJestConfig.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. const fs = require('fs');
  10. const chalk = require('react-dev-utils/chalk');
  11. const paths = require('../../config/paths');
  12. const modules = require('../../config/modules');
  13. module.exports = (resolve, rootDir, isEjecting) => {
  14. // Use this instead of `paths.testsSetup` to avoid putting
  15. // an absolute filename into configuration after ejecting.
  16. const setupTestsMatches = paths.testsSetup.match(/src[/\\]setupTests\.(.+)/);
  17. const setupTestsFileExtension =
  18. (setupTestsMatches && setupTestsMatches[1]) || 'js';
  19. const setupTestsFile = fs.existsSync(paths.testsSetup)
  20. ? `<rootDir>/src/setupTests.${setupTestsFileExtension}`
  21. : undefined;
  22. const config = {
  23. roots: ['<rootDir>/src'],
  24. collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts'],
  25. setupFiles: [
  26. isEjecting
  27. ? 'react-app-polyfill/jsdom'
  28. : require.resolve('react-app-polyfill/jsdom'),
  29. ],
  30. setupFilesAfterEnv: setupTestsFile ? [setupTestsFile] : [],
  31. testMatch: [
  32. '<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
  33. '<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}',
  34. ],
  35. testEnvironment: 'jsdom',
  36. testRunner: require.resolve('jest-circus/runner'),
  37. transform: {
  38. '^.+\\.(js|jsx|mjs|cjs|ts|tsx)$': resolve(
  39. 'config/jest/babelTransform.js'
  40. ),
  41. '^.+\\.css$': resolve('config/jest/cssTransform.js'),
  42. '^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)': resolve(
  43. 'config/jest/fileTransform.js'
  44. ),
  45. },
  46. transformIgnorePatterns: [
  47. '[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|cjs|ts|tsx)$',
  48. '^.+\\.module\\.(css|sass|scss)$',
  49. ],
  50. modulePaths: modules.additionalModulePaths || [],
  51. moduleNameMapper: {
  52. '^react-native$': 'react-native-web',
  53. '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
  54. ...(modules.jestAliases || {}),
  55. },
  56. moduleFileExtensions: [...paths.moduleFileExtensions, 'node'].filter(
  57. ext => !ext.includes('mjs')
  58. ),
  59. watchPlugins: [
  60. 'jest-watch-typeahead/filename',
  61. 'jest-watch-typeahead/testname',
  62. ],
  63. resetMocks: true,
  64. };
  65. if (rootDir) {
  66. config.rootDir = rootDir;
  67. }
  68. const overrides = Object.assign({}, require(paths.appPackageJson).jest);
  69. const supportedKeys = [
  70. 'clearMocks',
  71. 'collectCoverageFrom',
  72. 'coveragePathIgnorePatterns',
  73. 'coverageReporters',
  74. 'coverageThreshold',
  75. 'displayName',
  76. 'extraGlobals',
  77. 'globalSetup',
  78. 'globalTeardown',
  79. 'moduleNameMapper',
  80. 'resetMocks',
  81. 'resetModules',
  82. 'restoreMocks',
  83. 'snapshotSerializers',
  84. 'testMatch',
  85. 'transform',
  86. 'transformIgnorePatterns',
  87. 'watchPathIgnorePatterns',
  88. ];
  89. if (overrides) {
  90. supportedKeys.forEach(key => {
  91. if (Object.prototype.hasOwnProperty.call(overrides, key)) {
  92. if (Array.isArray(config[key]) || typeof config[key] !== 'object') {
  93. // for arrays or primitive types, directly override the config key
  94. config[key] = overrides[key];
  95. } else {
  96. // for object types, extend gracefully
  97. config[key] = Object.assign({}, config[key], overrides[key]);
  98. }
  99. delete overrides[key];
  100. }
  101. });
  102. const unsupportedKeys = Object.keys(overrides);
  103. if (unsupportedKeys.length) {
  104. const isOverridingSetupFile =
  105. unsupportedKeys.indexOf('setupFilesAfterEnv') > -1;
  106. if (isOverridingSetupFile) {
  107. console.error(
  108. chalk.red(
  109. 'We detected ' +
  110. chalk.bold('setupFilesAfterEnv') +
  111. ' in your package.json.\n\n' +
  112. 'Remove it from Jest configuration, and put the initialization code in ' +
  113. chalk.bold('src/setupTests.js') +
  114. '.\nThis file will be loaded automatically.\n'
  115. )
  116. );
  117. } else {
  118. console.error(
  119. chalk.red(
  120. '\nOut of the box, Create React App only supports overriding ' +
  121. 'these Jest options:\n\n' +
  122. supportedKeys
  123. .map(key => chalk.bold(' \u2022 ' + key))
  124. .join('\n') +
  125. '.\n\n' +
  126. 'These options in your package.json Jest configuration ' +
  127. 'are not currently supported by Create React App:\n\n' +
  128. unsupportedKeys
  129. .map(key => chalk.bold(' \u2022 ' + key))
  130. .join('\n') +
  131. '\n\nIf you wish to override other Jest options, you need to ' +
  132. 'eject from the default setup. You can do so by running ' +
  133. chalk.bold('npm run eject') +
  134. ' but remember that this is a one-way operation. ' +
  135. 'You may also file an issue with Create React App to discuss ' +
  136. 'supporting more options out of the box.\n'
  137. )
  138. );
  139. }
  140. process.exit(1);
  141. }
  142. }
  143. return config;
  144. };