You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

137 lines
4.7 KiB

  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('chalk');
  11. const paths = require('../../config/paths');
  12. module.exports = (resolve, rootDir, isEjecting) => {
  13. // Use this instead of `paths.testsSetup` to avoid putting
  14. // an absolute filename into configuration after ejecting.
  15. const setupTestsMatches = paths.testsSetup.match(/src\/setupTests\.(.+)/);
  16. const setupTestsFileExtension =
  17. (setupTestsMatches && setupTestsMatches[1]) || 'js';
  18. const setupTestsFile = fs.existsSync(paths.testsSetup)
  19. ? `<rootDir>/src/setupTests.${setupTestsFileExtension}`
  20. : undefined;
  21. // TODO: I don't know if it's safe or not to just use / as path separator
  22. // in Jest configs. We need help from somebody with Windows to determine this.
  23. const config = {
  24. collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts'],
  25. // TODO: this breaks Yarn PnP on eject.
  26. // But we can't simply emit this because it'll be an absolute path.
  27. // The proper fix is to write jest.config.js on eject instead of a package.json key.
  28. // Then these can always stay as require.resolve()s.
  29. resolver: isEjecting
  30. ? 'jest-pnp-resolver'
  31. : require.resolve('jest-pnp-resolver'),
  32. setupFiles: [
  33. isEjecting
  34. ? 'react-app-polyfill/jsdom'
  35. : require.resolve('react-app-polyfill/jsdom'),
  36. ],
  37. setupTestFrameworkScriptFile: setupTestsFile,
  38. testMatch: [
  39. '<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
  40. '<rootDir>/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}',
  41. ],
  42. testEnvironment: 'jsdom',
  43. testURL: 'http://localhost',
  44. transform: {
  45. '^.+\\.(js|jsx|ts|tsx)$': isEjecting
  46. ? '<rootDir>/node_modules/babel-jest'
  47. : resolve('config/jest/babelTransform.js'),
  48. '^.+\\.css$': resolve('config/jest/cssTransform.js'),
  49. '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': resolve(
  50. 'config/jest/fileTransform.js'
  51. ),
  52. },
  53. transformIgnorePatterns: [
  54. '[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$',
  55. '^.+\\.module\\.(css|sass|scss)$',
  56. ],
  57. moduleNameMapper: {
  58. '^react-native$': 'react-native-web',
  59. '^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
  60. },
  61. moduleFileExtensions: [...paths.moduleFileExtensions, 'node'].filter(
  62. ext => !ext.includes('mjs')
  63. ),
  64. };
  65. if (rootDir) {
  66. config.rootDir = rootDir;
  67. }
  68. const overrides = Object.assign({}, require(paths.appPackageJson).jest);
  69. const supportedKeys = [
  70. 'collectCoverageFrom',
  71. 'coverageReporters',
  72. 'coverageThreshold',
  73. 'globalSetup',
  74. 'globalTeardown',
  75. 'resetMocks',
  76. 'resetModules',
  77. 'snapshotSerializers',
  78. 'watchPathIgnorePatterns',
  79. ];
  80. if (overrides) {
  81. supportedKeys.forEach(key => {
  82. if (overrides.hasOwnProperty(key)) {
  83. config[key] = overrides[key];
  84. delete overrides[key];
  85. }
  86. });
  87. const unsupportedKeys = Object.keys(overrides);
  88. if (unsupportedKeys.length) {
  89. const isOverridingSetupFile =
  90. unsupportedKeys.indexOf('setupTestFrameworkScriptFile') > -1;
  91. if (isOverridingSetupFile) {
  92. console.error(
  93. chalk.red(
  94. 'We detected ' +
  95. chalk.bold('setupTestFrameworkScriptFile') +
  96. ' in your package.json.\n\n' +
  97. 'Remove it from Jest configuration, and put the initialization code in ' +
  98. chalk.bold('src/setupTests.js') +
  99. '.\nThis file will be loaded automatically.\n'
  100. )
  101. );
  102. } else {
  103. console.error(
  104. chalk.red(
  105. '\nOut of the box, Create React App only supports overriding ' +
  106. 'these Jest options:\n\n' +
  107. supportedKeys
  108. .map(key => chalk.bold(' \u2022 ' + key))
  109. .join('\n') +
  110. '.\n\n' +
  111. 'These options in your package.json Jest configuration ' +
  112. 'are not currently supported by Create React App:\n\n' +
  113. unsupportedKeys
  114. .map(key => chalk.bold(' \u2022 ' + key))
  115. .join('\n') +
  116. '\n\nIf you wish to override other Jest options, you need to ' +
  117. 'eject from the default setup. You can do so by running ' +
  118. chalk.bold('npm run eject') +
  119. ' but remember that this is a one-way operation. ' +
  120. 'You may also file an issue with Create React App to discuss ' +
  121. 'supporting more options out of the box.\n'
  122. )
  123. );
  124. }
  125. process.exit(1);
  126. }
  127. }
  128. return config;
  129. };