getMaxWorkers.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = getMaxWorkers;
  6. function _os() {
  7. const data = require('os');
  8. _os = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. /**
  14. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  15. *
  16. * This source code is licensed under the MIT license found in the
  17. * LICENSE file in the root directory of this source tree.
  18. */
  19. function getMaxWorkers(argv, defaultOptions) {
  20. if (argv.runInBand) {
  21. return 1;
  22. } else if (argv.maxWorkers) {
  23. return parseWorkers(argv.maxWorkers);
  24. } else if (defaultOptions && defaultOptions.maxWorkers) {
  25. return parseWorkers(defaultOptions.maxWorkers);
  26. } else {
  27. // In watch mode, Jest should be unobtrusive and not use all available CPUs.
  28. const numCpus = (0, _os().cpus)() ? (0, _os().cpus)().length : 1;
  29. const isWatchModeEnabled = argv.watch || argv.watchAll;
  30. return Math.max(
  31. isWatchModeEnabled ? Math.floor(numCpus / 2) : numCpus - 1,
  32. 1
  33. );
  34. }
  35. }
  36. const parseWorkers = maxWorkers => {
  37. const parsed = parseInt(maxWorkers.toString(), 10);
  38. if (
  39. typeof maxWorkers === 'string' &&
  40. maxWorkers.trim().endsWith('%') &&
  41. parsed > 0 &&
  42. parsed <= 100
  43. ) {
  44. const numCpus = (0, _os().cpus)().length;
  45. const workers = Math.floor((parsed / 100) * numCpus);
  46. return workers >= 1 ? workers : 1;
  47. }
  48. return parsed > 0 ? parsed : 1;
  49. };