createJestConfig.js 4.9 KB

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