Skip to content

Commit 9e2c7b1

Browse files
authored
jest-worker: Unable to customize thread execArgv with enableThreadWorkers (#12069)
1 parent 2e6c217 commit 9e2c7b1

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

packages/jest-worker/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export type WorkerOptions = {
125125
setupArgs: Array<unknown>;
126126
maxRetries: number;
127127
workerId: number;
128+
workerData?: unknown;
128129
workerPath: string;
129130
};
130131

packages/jest-worker/src/workers/NodeThreadsWorker.ts

+10-16
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77

88
import * as path from 'path';
99
import {PassThrough} from 'stream';
10-
import {
11-
Worker,
12-
WorkerOptions as WorkerThreadsWorkerOptions,
13-
} from 'worker_threads';
10+
import {Worker} from 'worker_threads';
1411
import mergeStream = require('merge-stream');
1512
import {
1613
CHILD_MESSAGE_INITIALIZE,
@@ -63,22 +60,19 @@ export default class ExperimentalWorker implements WorkerInterface {
6360

6461
initialize(): void {
6562
this._worker = new Worker(path.resolve(__dirname, './threadChild.js'), {
63+
env: {
64+
...process.env,
65+
JEST_WORKER_ID: String(this._options.workerId + 1), // 0-indexed workerId, 1-indexed JEST_WORKER_ID
66+
},
6667
eval: false,
68+
execArgv: process.execArgv,
69+
// @ts-expect-error: added in newer versions
6770
resourceLimits: this._options.resourceLimits,
6871
stderr: true,
6972
stdout: true,
70-
workerData: {
71-
cwd: process.cwd(),
72-
env: {
73-
...process.env,
74-
JEST_WORKER_ID: String(this._options.workerId + 1), // 0-indexed workerId, 1-indexed JEST_WORKER_ID
75-
} as NodeJS.ProcessEnv,
76-
// Suppress --debug / --inspect flags while preserving others (like --harmony).
77-
execArgv: process.execArgv.filter(v => !/^--(debug|inspect)/.test(v)),
78-
silent: true,
79-
...this._options.forkOptions,
80-
},
81-
} as WorkerThreadsWorkerOptions);
73+
workerData: this._options.workerData,
74+
...this._options.forkOptions,
75+
});
8276

8377
if (this._worker.stdout) {
8478
if (!this._stdout) {

packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,36 @@ afterEach(() => {
5151
process.execArgv = originalExecArgv;
5252
});
5353

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

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

5959
// eslint-disable-next-line no-new
6060
new Worker({
6161
forkOptions: {
62-
cwd: '/tmp',
6362
execPath: 'hello',
6463
},
6564
maxRetries: 3,
65+
workerData: {
66+
foo: 'bar',
67+
},
6668
workerId: process.env.JEST_WORKER_ID - 1,
6769
workerPath: '/tmp/foo/bar/baz.js',
6870
});
6971

7072
expect(workerThreads.mock.calls[0][0]).toBe(thread.replace(/\.ts$/, '.js'));
7173
expect(workerThreads.mock.calls[0][1]).toEqual({
74+
env: process.env, // Default option.
7275
eval: false,
76+
execArgv: ['--inspect', '-p'],
77+
execPath: 'hello', // Added option.
78+
resourceLimits: undefined,
7379
stderr: true,
7480
stdout: true,
7581
workerData: {
76-
cwd: '/tmp', // Overridden default option.
77-
env: process.env, // Default option.
78-
execArgv: ['-p'], // Filtered option.
79-
execPath: 'hello', // Added option.
80-
silent: true, // Default option.
82+
// Added option.
83+
foo: 'bar',
8184
},
8285
});
8386
});
@@ -91,9 +94,7 @@ it('passes workerId to the thread and assign it to env.JEST_WORKER_ID', () => {
9194
workerPath: '/tmp/foo',
9295
});
9396

94-
expect(workerThreads.mock.calls[0][1].workerData.env.JEST_WORKER_ID).toEqual(
95-
'3',
96-
);
97+
expect(workerThreads.mock.calls[0][1].env.JEST_WORKER_ID).toEqual('3');
9798
});
9899

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

0 commit comments

Comments
 (0)