testSchedulerHelper.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.shouldRunInBand = shouldRunInBand;
  6. /**
  7. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  8. *
  9. * This source code is licensed under the MIT license found in the
  10. * LICENSE file in the root directory of this source tree.
  11. */
  12. const SLOW_TEST_TIME = 1000;
  13. function shouldRunInBand(
  14. tests,
  15. timings,
  16. {detectOpenHandles, maxWorkers, watch, watchAll}
  17. ) {
  18. // detectOpenHandles makes no sense without runInBand, because it cannot detect leaks in workers
  19. if (detectOpenHandles) {
  20. return true;
  21. }
  22. /*
  23. * Run in band if we only have one test or one worker available, unless we
  24. * are using the watch mode, in which case the TTY has to be responsive and
  25. * we cannot schedule anything in the main thread. Same logic applies to
  26. * watchAll.
  27. * Also, if we are confident from previous runs that the tests will finish
  28. * quickly we also run in band to reduce the overhead of spawning workers.
  29. * Finally, the user can provide the runInBand argument in the CLI to
  30. * force running in band.
  31. * https://github.com/facebook/jest/blob/700e0dadb85f5dc8ff5dac6c7e98956690049734/packages/jest-config/src/getMaxWorkers.js#L14-L17
  32. */
  33. const isWatchMode = watch || watchAll;
  34. const areFastTests = timings.every(timing => timing < SLOW_TEST_TIME);
  35. const oneWorkerOrLess = maxWorkers <= 1;
  36. const oneTestOrLess = tests.length <= 1;
  37. if (isWatchMode) {
  38. return oneWorkerOrLess || (oneTestOrLess && areFastTests);
  39. }
  40. return (
  41. oneWorkerOrLess ||
  42. oneTestOrLess ||
  43. (tests.length <= 20 && timings.length > 0 && areFastTests)
  44. );
  45. }