Skip to content

Commit

Permalink
feat(jest-worker): support passing through ResourceLimits to worker (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jul 30, 2020
1 parent eb23ac1 commit c7b1834
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
- `[jest-core, jest-circus, jest-reporter, jest-runner]` Added support for reporting individual test cases using jest-circus ([#10227](https://github.com/facebook/jest/pull/10227))
- `[jest-config, jest-reporter, jest-runner, jest-test-sequencer]` Add `slowTestThreshold` configuration option ([#9366](https://github.com/facebook/jest/pull/9366))
- `[jest-worker]` Added support for workers to send custom messages to parent in jest-worker ([#10293](https://github.com/facebook/jest/pull/10293))
- `[jest-worker]` Support passing `resourceLimits` ([#10335](https://github.com/facebook/jest/pull/10335))
- `[pretty-format]` Added support for serializing custom elements (web components) ([#10217](https://github.com/facebook/jest/pull/10237))

### Fixes
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-worker/src/base/BaseWorkerPool.ts
Expand Up @@ -40,12 +40,13 @@ export default class BaseWorkerPool {
const stdout = mergeStream();
const stderr = mergeStream();

const {forkOptions, maxRetries, setupArgs} = options;
const {forkOptions, maxRetries, resourceLimits, setupArgs} = options;

for (let i = 0; i < options.numWorkers; i++) {
const workerOptions: WorkerOptions = {
forkOptions,
maxRetries,
resourceLimits,
setupArgs,
workerId: i,
workerPath,
Expand Down
11 changes: 6 additions & 5 deletions packages/jest-worker/src/index.ts
Expand Up @@ -76,11 +76,12 @@ export default class JestWorker {
this._ending = false;

const workerPoolOptions: WorkerPoolOptions = {
enableWorkerThreads: this._options.enableWorkerThreads || false,
forkOptions: this._options.forkOptions || {},
maxRetries: this._options.maxRetries || 3,
numWorkers: this._options.numWorkers || Math.max(cpus().length - 1, 1),
setupArgs: this._options.setupArgs || [],
enableWorkerThreads: this._options.enableWorkerThreads ?? false,
forkOptions: this._options.forkOptions ?? {},
maxRetries: this._options.maxRetries ?? 3,
numWorkers: this._options.numWorkers ?? Math.max(cpus().length - 1, 1),
resourceLimits: this._options.resourceLimits ?? {},
setupArgs: this._options.setupArgs ?? [],
};

if (this._options.WorkerPool) {
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-worker/src/types.ts
Expand Up @@ -7,6 +7,7 @@

import type {EventEmitter} from 'events';
import type {ForkOptions} from 'child_process';
import type {ResourceLimits} from 'worker_threads';

// Because of the dynamic nature of a worker communication process, all messages
// coming from any of the other processes cannot be typed. Thus, many types
Expand Down Expand Up @@ -65,12 +66,13 @@ export interface PromiseWithCustomMessage<T> extends Promise<T> {

// Option objects.

export type {ForkOptions};
export type {ForkOptions, ResourceLimits};

export type FarmOptions = {
computeWorkerKey?: (method: string, ...args: Array<unknown>) => string | null;
exposedMethods?: ReadonlyArray<string>;
forkOptions?: ForkOptions;
resourceLimits?: ResourceLimits;
setupArgs?: Array<unknown>;
maxRetries?: number;
numWorkers?: number;
Expand All @@ -84,13 +86,15 @@ export type FarmOptions = {
export type WorkerPoolOptions = {
setupArgs: Array<unknown>;
forkOptions: ForkOptions;
resourceLimits: ResourceLimits;
maxRetries: number;
numWorkers: number;
enableWorkerThreads: boolean;
};

export type WorkerOptions = {
forkOptions: ForkOptions;
resourceLimits: ResourceLimits;
setupArgs: Array<unknown>;
maxRetries: number;
workerId: number;
Expand Down
1 change: 1 addition & 0 deletions packages/jest-worker/src/workers/NodeThreadsWorker.ts
Expand Up @@ -62,6 +62,7 @@ export default class ExperimentalWorker implements WorkerInterface {
initialize(): void {
this._worker = new Worker(path.resolve(__dirname, './threadChild.js'), {
eval: false,
resourceLimits: this._options.resourceLimits,
stderr: true,
stdout: true,
workerData: {
Expand Down

0 comments on commit c7b1834

Please sign in to comment.