test.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. // Do this as the first thing so that any code reading it knows the right env.
  3. process.env.BABEL_ENV = 'test';
  4. process.env.NODE_ENV = 'test';
  5. process.env.PUBLIC_URL = '';
  6. // Makes the script crash on unhandled rejections instead of silently
  7. // ignoring them. In the future, promise rejections that are not handled will
  8. // terminate the Node.js process with a non-zero exit code.
  9. process.on('unhandledRejection', err => {
  10. throw err;
  11. });
  12. // Ensure environment variables are read.
  13. require('../config/env');
  14. const jest = require('jest');
  15. const execSync = require('child_process').execSync;
  16. let argv = process.argv.slice(2);
  17. function isInGitRepository() {
  18. try {
  19. execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
  20. return true;
  21. } catch (e) {
  22. return false;
  23. }
  24. }
  25. function isInMercurialRepository() {
  26. try {
  27. execSync('hg --cwd . root', { stdio: 'ignore' });
  28. return true;
  29. } catch (e) {
  30. return false;
  31. }
  32. }
  33. // Watch unless on CI or explicitly running all tests
  34. if (
  35. !process.env.CI &&
  36. argv.indexOf('--watchAll') === -1 &&
  37. argv.indexOf('--watchAll=false') === -1
  38. ) {
  39. // https://github.com/facebook/create-react-app/issues/5210
  40. const hasSourceControl = isInGitRepository() || isInMercurialRepository();
  41. argv.push(hasSourceControl ? '--watch' : '--watchAll');
  42. }
  43. jest.run(argv);