Skip to content

Commit

Permalink
jest-worker: Unable to customize thread execArgv with enableThreadWor…
Browse files Browse the repository at this point in the history
…kers (#12069)
  • Loading branch information
kherock authored Nov 29, 2021
1 parent 2e6c217 commit 9e2c7b1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/jest-worker/src/types.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 9e2c7b1

Please sign in to comment.