Skip to content

Commit 7148872

Browse files
[jest-worker] Don't pass explicit env to new Worker when using worker_threads (#12141)
1 parent 07c2c15 commit 7148872

File tree

5 files changed

+23
-18
lines changed

5 files changed

+23
-18
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
### Performance
1010

11+
## 27.4.5
12+
13+
### Fixes
14+
15+
- `[jest-worker]` Stop explicitly passing `process.env` ([#12141](https://github.com/facebook/jest/pull/12141))
16+
1117
## 27.4.4
1218

1319
### Fixes

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ export default class ExperimentalWorker implements WorkerInterface {
6060

6161
initialize(): void {
6262
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-
},
6763
eval: false,
6864
// @ts-expect-error: added in newer versions
6965
resourceLimits: this._options.resourceLimits,
@@ -101,6 +97,7 @@ export default class ExperimentalWorker implements WorkerInterface {
10197
false,
10298
this._options.workerPath,
10399
this._options.setupArgs,
100+
String(this._options.workerId + 1), // 0-indexed workerId, 1-indexed JEST_WORKER_ID
104101
]);
105102

106103
this._retries++;

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

+3-14
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ it('passes fork options down to worker_threads.Worker, adding the defaults', ()
7070

7171
expect(workerThreads.mock.calls[0][0]).toBe(thread.replace(/\.ts$/, '.js'));
7272
expect(workerThreads.mock.calls[0][1]).toEqual({
73-
env: process.env, // Default option.
7473
eval: false,
7574
execArgv: ['--inspect', '-p'],
7675
execPath: 'hello', // Added option.
@@ -84,23 +83,12 @@ it('passes fork options down to worker_threads.Worker, adding the defaults', ()
8483
});
8584
});
8685

87-
it('passes workerId to the thread and assign it to env.JEST_WORKER_ID', () => {
88-
// eslint-disable-next-line no-new
89-
new Worker({
90-
forkOptions: {},
91-
maxRetries: 3,
92-
workerId: 2,
93-
workerPath: '/tmp/foo',
94-
});
95-
96-
expect(workerThreads.mock.calls[0][1].env.JEST_WORKER_ID).toEqual('3');
97-
});
98-
99-
it('initializes the thread with the given workerPath', () => {
86+
it('initializes the thread with the given workerPath and workerId', () => {
10087
const worker = new Worker({
10188
forkOptions: {},
10289
maxRetries: 3,
10390
setupArgs: ['foo', 'bar'],
91+
workerId: 2,
10492
workerPath: '/tmp/foo/bar/baz.js',
10593
});
10694

@@ -109,6 +97,7 @@ it('initializes the thread with the given workerPath', () => {
10997
false,
11098
'/tmp/foo/bar/baz.js',
11199
['foo', 'bar'],
100+
'3',
112101
]);
113102
});
114103

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

+12
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ afterEach(() => {
128128
thread.removeAllListeners('message');
129129
});
130130

131+
it('sets env.JEST_WORKER_ID', () => {
132+
thread.emit('message', [
133+
CHILD_MESSAGE_INITIALIZE,
134+
true, // Not really used here, but for flow type purity.
135+
'./my-fancy-worker',
136+
[],
137+
'3',
138+
]);
139+
140+
expect(process.env.JEST_WORKER_ID).toBe('3');
141+
});
142+
131143
it('lazily requires the file', () => {
132144
expect(mockCount).toBe(0);
133145

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

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const messageListener = (request: any) => {
4141
const init: ChildMessageInitialize = request;
4242
file = init[2];
4343
setupArgs = request[3];
44+
process.env.JEST_WORKER_ID = request[4];
4445
break;
4546

4647
case CHILD_MESSAGE_CALL:

0 commit comments

Comments
 (0)