test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. const jest = require('jest');
  23. const execSync = require('child_process').execSync;
  24. let argv = process.argv.slice(2);
  25. function isInGitRepository() {
  26. try {
  27. execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
  28. return true;
  29. } catch (e) {
  30. return false;
  31. }
  32. }
  33. function isInMercurialRepository() {
  34. try {
  35. execSync('hg --cwd . root', { stdio: 'ignore' });
  36. return true;
  37. } catch (e) {
  38. return false;
  39. }
  40. }
  41. // Watch unless on CI or explicitly running all tests
  42. if (
  43. !process.env.CI &&
  44. argv.indexOf('--watchAll') === -1 &&
  45. argv.indexOf('--watchAll=false') === -1
  46. ) {
  47. // https://github.com/facebook/create-react-app/issues/5210
  48. const hasSourceControl = isInGitRepository() || isInMercurialRepository();
  49. argv.push(hasSourceControl ? '--watch' : '--watchAll');
  50. }
  51. // @remove-on-eject-begin
  52. // This is not necessary after eject because we embed config into package.json.
  53. const createJestConfig = require('./utils/createJestConfig');
  54. const path = require('path');
  55. const paths = require('../config/paths');
  56. argv.push(
  57. '--config',
  58. JSON.stringify(
  59. createJestConfig(
  60. relativePath => path.resolve(__dirname, '..', relativePath),
  61. path.resolve(paths.appSrc, '..'),
  62. false
  63. )
  64. )
  65. );
  66. // This is a very dirty workaround for https://github.com/facebook/jest/issues/5913.
  67. // We're trying to resolve the environment ourselves because Jest does it incorrectly.
  68. // TODO: remove this as soon as it's fixed in Jest.
  69. const resolve = require('resolve');
  70. function resolveJestDefaultEnvironment(name) {
  71. const jestDir = path.dirname(
  72. resolve.sync('jest', {
  73. basedir: __dirname,
  74. })
  75. );
  76. const jestCLIDir = path.dirname(
  77. resolve.sync('jest-cli', {
  78. basedir: jestDir,
  79. })
  80. );
  81. const jestConfigDir = path.dirname(
  82. resolve.sync('jest-config', {
  83. basedir: jestCLIDir,
  84. })
  85. );
  86. return resolve.sync(name, {
  87. basedir: jestConfigDir,
  88. });
  89. }
  90. let cleanArgv = [];
  91. let env = 'jsdom';
  92. let next;
  93. do {
  94. next = argv.shift();
  95. if (next === '--env') {
  96. env = argv.shift();
  97. } else if (next.indexOf('--env=') === 0) {
  98. env = next.substring('--env='.length);
  99. } else {
  100. cleanArgv.push(next);
  101. }
  102. } while (argv.length > 0);
  103. argv = cleanArgv;
  104. let resolvedEnv;
  105. try {
  106. resolvedEnv = resolveJestDefaultEnvironment(`jest-environment-${env}`);
  107. } catch (e) {
  108. // ignore
  109. }
  110. if (!resolvedEnv) {
  111. try {
  112. resolvedEnv = resolveJestDefaultEnvironment(env);
  113. } catch (e) {
  114. // ignore
  115. }
  116. }
  117. const testEnvironment = resolvedEnv || env;
  118. argv.push('--env', testEnvironment);
  119. // @remove-on-eject-end
  120. jest.run(argv);