test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // @remove-on-eject-begin
  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. // @remove-on-eject-end
  9. 'use strict';
  10. // Do this as the first thing so that any code reading it knows the right env.
  11. process.env.BABEL_ENV = 'test';
  12. process.env.NODE_ENV = 'test';
  13. process.env.PUBLIC_URL = '';
  14. // Makes the script crash on unhandled rejections instead of silently
  15. // ignoring them. In the future, promise rejections that are not handled will
  16. // terminate the Node.js process with a non-zero exit code.
  17. process.on('unhandledRejection', err => {
  18. throw err;
  19. });
  20. // Ensure environment variables are read.
  21. require('../config/env');
  22. // @remove-on-eject-begin
  23. // Do the preflight check (only happens before eject).
  24. const verifyPackageTree = require('./utils/verifyPackageTree');
  25. if (process.env.SKIP_PREFLIGHT_CHECK !== 'true') {
  26. verifyPackageTree();
  27. }
  28. const verifyTypeScriptSetup = require('./utils/verifyTypeScriptSetup');
  29. verifyTypeScriptSetup();
  30. // @remove-on-eject-end
  31. const jest = require('jest');
  32. const execSync = require('child_process').execSync;
  33. let argv = process.argv.slice(2);
  34. function isInGitRepository() {
  35. try {
  36. execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
  37. return true;
  38. } catch (e) {
  39. return false;
  40. }
  41. }
  42. function isInMercurialRepository() {
  43. try {
  44. execSync('hg --cwd . root', { stdio: 'ignore' });
  45. return true;
  46. } catch (e) {
  47. return false;
  48. }
  49. }
  50. // Watch unless on CI or explicitly running all tests
  51. if (
  52. !process.env.CI &&
  53. argv.indexOf('--watchAll') === -1 &&
  54. argv.indexOf('--watchAll=false') === -1
  55. ) {
  56. // https://github.com/facebook/create-react-app/issues/5210
  57. const hasSourceControl = isInGitRepository() || isInMercurialRepository();
  58. argv.push(hasSourceControl ? '--watch' : '--watchAll');
  59. }
  60. // @remove-on-eject-begin
  61. // This is not necessary after eject because we embed config into package.json.
  62. const createJestConfig = require('./utils/createJestConfig');
  63. const path = require('path');
  64. const paths = require('../config/paths');
  65. argv.push(
  66. '--config',
  67. JSON.stringify(
  68. createJestConfig(
  69. relativePath => path.resolve(__dirname, '..', relativePath),
  70. path.resolve(paths.appSrc, '..'),
  71. false
  72. )
  73. )
  74. );
  75. // This is a very dirty workaround for https://github.com/facebook/jest/issues/5913.
  76. // We're trying to resolve the environment ourselves because Jest does it incorrectly.
  77. // TODO: remove this as soon as it's fixed in Jest.
  78. const resolve = require('resolve');
  79. function resolveJestDefaultEnvironment(name) {
  80. const jestDir = path.dirname(
  81. resolve.sync('jest', {
  82. basedir: __dirname,
  83. })
  84. );
  85. const jestCLIDir = path.dirname(
  86. resolve.sync('jest-cli', {
  87. basedir: jestDir,
  88. })
  89. );
  90. const jestConfigDir = path.dirname(
  91. resolve.sync('jest-config', {
  92. basedir: jestCLIDir,
  93. })
  94. );
  95. return resolve.sync(name, {
  96. basedir: jestConfigDir,
  97. });
  98. }
  99. let cleanArgv = [];
  100. let env = 'jsdom';
  101. let next;
  102. do {
  103. next = argv.shift();
  104. if (next === '--env') {
  105. env = argv.shift();
  106. } else if (next.indexOf('--env=') === 0) {
  107. env = next.substring('--env='.length);
  108. } else {
  109. cleanArgv.push(next);
  110. }
  111. } while (argv.length > 0);
  112. argv = cleanArgv;
  113. let resolvedEnv;
  114. try {
  115. resolvedEnv = resolveJestDefaultEnvironment(`jest-environment-${env}`);
  116. } catch (e) {
  117. // ignore
  118. }
  119. if (!resolvedEnv) {
  120. try {
  121. resolvedEnv = resolveJestDefaultEnvironment(env);
  122. } catch (e) {
  123. // ignore
  124. }
  125. }
  126. const testEnvironment = resolvedEnv || env;
  127. argv.push('--env', testEnvironment);
  128. // @remove-on-eject-end
  129. jest.run(argv);