index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _os() {
  7. const data = require('os');
  8. _os = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. var _WorkerPool = _interopRequireDefault(require('./WorkerPool'));
  14. var _Farm = _interopRequireDefault(require('./Farm'));
  15. function _interopRequireDefault(obj) {
  16. return obj && obj.__esModule ? obj : {default: obj};
  17. }
  18. function _defineProperty(obj, key, value) {
  19. if (key in obj) {
  20. Object.defineProperty(obj, key, {
  21. value: value,
  22. enumerable: true,
  23. configurable: true,
  24. writable: true
  25. });
  26. } else {
  27. obj[key] = value;
  28. }
  29. return obj;
  30. }
  31. function getExposedMethods(workerPath, options) {
  32. let exposedMethods = options.exposedMethods; // If no methods list is given, try getting it by auto-requiring the module.
  33. if (!exposedMethods) {
  34. const module = require(workerPath);
  35. exposedMethods = Object.keys(module).filter(
  36. // @ts-ignore: no index
  37. name => typeof module[name] === 'function'
  38. );
  39. if (typeof module === 'function') {
  40. exposedMethods = [...exposedMethods, 'default'];
  41. }
  42. }
  43. return exposedMethods;
  44. }
  45. /**
  46. * The Jest farm (publicly called "Worker") is a class that allows you to queue
  47. * methods across multiple child processes, in order to parallelize work. This
  48. * is done by providing an absolute path to a module that will be loaded on each
  49. * of the child processes, and bridged to the main process.
  50. *
  51. * Bridged methods are specified by using the "exposedMethods" property of the
  52. * "options" object. This is an array of strings, where each of them corresponds
  53. * to the exported name in the loaded module.
  54. *
  55. * You can also control the amount of workers by using the "numWorkers" property
  56. * of the "options" object, and the settings passed to fork the process through
  57. * the "forkOptions" property. The amount of workers defaults to the amount of
  58. * CPUS minus one.
  59. *
  60. * Queueing calls can be done in two ways:
  61. * - Standard method: calls will be redirected to the first available worker,
  62. * so they will get executed as soon as they can.
  63. *
  64. * - Sticky method: if a "computeWorkerKey" method is provided within the
  65. * config, the resulting string of this method will be used as a key.
  66. * Every time this key is returned, it is guaranteed that your job will be
  67. * processed by the same worker. This is specially useful if your workers
  68. * are caching results.
  69. */
  70. class JestWorker {
  71. constructor(workerPath, options) {
  72. _defineProperty(this, '_ending', void 0);
  73. _defineProperty(this, '_farm', void 0);
  74. _defineProperty(this, '_options', void 0);
  75. _defineProperty(this, '_workerPool', void 0);
  76. this._options = {...options};
  77. this._ending = false;
  78. const workerPoolOptions = {
  79. enableWorkerThreads: this._options.enableWorkerThreads || false,
  80. forkOptions: this._options.forkOptions || {},
  81. maxRetries: this._options.maxRetries || 3,
  82. numWorkers:
  83. this._options.numWorkers || Math.max((0, _os().cpus)().length - 1, 1),
  84. setupArgs: this._options.setupArgs || []
  85. };
  86. if (this._options.WorkerPool) {
  87. // @ts-ignore: constructor target any?
  88. this._workerPool = new this._options.WorkerPool(
  89. workerPath,
  90. workerPoolOptions
  91. );
  92. } else {
  93. this._workerPool = new _WorkerPool.default(workerPath, workerPoolOptions);
  94. }
  95. this._farm = new _Farm.default(
  96. workerPoolOptions.numWorkers,
  97. this._workerPool.send.bind(this._workerPool),
  98. this._options.computeWorkerKey
  99. );
  100. this._bindExposedWorkerMethods(workerPath, this._options);
  101. }
  102. _bindExposedWorkerMethods(workerPath, options) {
  103. getExposedMethods(workerPath, options).forEach(name => {
  104. if (name.startsWith('_')) {
  105. return;
  106. }
  107. if (this.constructor.prototype.hasOwnProperty(name)) {
  108. throw new TypeError('Cannot define a method called ' + name);
  109. } // @ts-ignore: dynamic extension of the class instance is expected.
  110. this[name] = this._callFunctionWithArgs.bind(this, name);
  111. });
  112. }
  113. _callFunctionWithArgs(method, ...args) {
  114. if (this._ending) {
  115. throw new Error('Farm is ended, no more calls can be done to it');
  116. }
  117. return this._farm.doWork(method, ...args);
  118. }
  119. getStderr() {
  120. return this._workerPool.getStderr();
  121. }
  122. getStdout() {
  123. return this._workerPool.getStdout();
  124. }
  125. async end() {
  126. if (this._ending) {
  127. throw new Error('Farm is ended, no more calls can be done to it');
  128. }
  129. this._ending = true;
  130. return this._workerPool.end();
  131. }
  132. }
  133. exports.default = JestWorker;