Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest-worker): support passing through ResourceLimits to worker #10335

Merged
merged 2 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

- `[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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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