test.js 1.3 KB

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