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

jest-worker: Unable to customize thread execArgv with enableThreadWorkers #12069

Merged
merged 5 commits into from Nov 29, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/jest-worker/src/types.ts
Expand Up @@ -125,6 +125,7 @@ export type WorkerOptions = {
setupArgs: Array<unknown>;
maxRetries: number;
workerId: number;
workerData?: unknown;
workerPath: string;
};

Expand Down
26 changes: 10 additions & 16 deletions packages/jest-worker/src/workers/NodeThreadsWorker.ts
Expand Up @@ -7,10 +7,7 @@

import * as path from 'path';
import {PassThrough} from 'stream';
import {
Worker,
WorkerOptions as WorkerThreadsWorkerOptions,
} from 'worker_threads';
import {Worker} from 'worker_threads';
import mergeStream = require('merge-stream');
import {
CHILD_MESSAGE_INITIALIZE,
Expand Down Expand Up @@ -63,22 +60,19 @@ export default class ExperimentalWorker implements WorkerInterface {

initialize(): void {
this._worker = new Worker(path.resolve(__dirname, './threadChild.js'), {
env: {
...process.env,
JEST_WORKER_ID: String(this._options.workerId + 1), // 0-indexed workerId, 1-indexed JEST_WORKER_ID
},
eval: false,
execArgv: process.execArgv,
// @ts-expect-error: added in newer versions
resourceLimits: this._options.resourceLimits,
stderr: true,
stdout: true,
workerData: {
cwd: process.cwd(),
env: {
...process.env,
JEST_WORKER_ID: String(this._options.workerId + 1), // 0-indexed workerId, 1-indexed JEST_WORKER_ID
} as NodeJS.ProcessEnv,
// Suppress --debug / --inspect flags while preserving others (like --harmony).
execArgv: process.execArgv.filter(v => !/^--(debug|inspect)/.test(v)),
silent: true,
...this._options.forkOptions,
},
} as WorkerThreadsWorkerOptions);
workerData: this._options.workerData,
...this._options.forkOptions,
});

if (this._worker.stdout) {
if (!this._stdout) {
Expand Down
Expand Up @@ -51,33 +51,36 @@ afterEach(() => {
process.execArgv = originalExecArgv;
});

it('passes fork options down to child_process.fork, adding the defaults', () => {
it('passes fork options down to worker_threads.Worker, adding the defaults', () => {
const thread = require.resolve('../threadChild');

process.execArgv = ['--inspect', '-p'];

// eslint-disable-next-line no-new
new Worker({
forkOptions: {
cwd: '/tmp',
execPath: 'hello',
},
maxRetries: 3,
workerData: {
foo: 'bar',
},
workerId: process.env.JEST_WORKER_ID - 1,
workerPath: '/tmp/foo/bar/baz.js',
});

expect(workerThreads.mock.calls[0][0]).toBe(thread.replace(/\.ts$/, '.js'));
expect(workerThreads.mock.calls[0][1]).toEqual({
env: process.env, // Default option.
eval: false,
execArgv: ['--inspect', '-p'],
execPath: 'hello', // Added option.
resourceLimits: undefined,
stderr: true,
stdout: true,
workerData: {
cwd: '/tmp', // Overridden default option.
env: process.env, // Default option.
execArgv: ['-p'], // Filtered option.
execPath: 'hello', // Added option.
silent: true, // Default option.
// Added option.
foo: 'bar',
},
});
});
Expand All @@ -91,9 +94,7 @@ it('passes workerId to the thread and assign it to env.JEST_WORKER_ID', () => {
workerPath: '/tmp/foo',
});

expect(workerThreads.mock.calls[0][1].workerData.env.JEST_WORKER_ID).toEqual(
'3',
);
expect(workerThreads.mock.calls[0][1].env.JEST_WORKER_ID).toEqual('3');
});

it('initializes the thread with the given workerPath', () => {
Expand Down