diff --git a/CHANGELOG.md b/CHANGELOG.md index 01f8245f6fa1..c12809c402ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ ### Performance +## 27.4.5 + +### Fixes + +- `[jest-worker]` Stop explicitly passing `process.env` ([#12141](https://github.com/facebook/jest/pull/12141)) + ## 27.4.4 ### Fixes diff --git a/packages/jest-worker/src/workers/NodeThreadsWorker.ts b/packages/jest-worker/src/workers/NodeThreadsWorker.ts index 780f85dd2156..136b648c33de 100644 --- a/packages/jest-worker/src/workers/NodeThreadsWorker.ts +++ b/packages/jest-worker/src/workers/NodeThreadsWorker.ts @@ -60,10 +60,6 @@ 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, // @ts-expect-error: added in newer versions resourceLimits: this._options.resourceLimits, @@ -101,6 +97,7 @@ export default class ExperimentalWorker implements WorkerInterface { false, this._options.workerPath, this._options.setupArgs, + String(this._options.workerId + 1), // 0-indexed workerId, 1-indexed JEST_WORKER_ID ]); this._retries++; diff --git a/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js b/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js index 3ff5f2b6a874..131ee206ad0d 100644 --- a/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js +++ b/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js @@ -70,7 +70,6 @@ it('passes fork options down to worker_threads.Worker, adding the defaults', () 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. @@ -84,23 +83,12 @@ it('passes fork options down to worker_threads.Worker, adding the defaults', () }); }); -it('passes workerId to the thread and assign it to env.JEST_WORKER_ID', () => { - // eslint-disable-next-line no-new - new Worker({ - forkOptions: {}, - maxRetries: 3, - workerId: 2, - workerPath: '/tmp/foo', - }); - - expect(workerThreads.mock.calls[0][1].env.JEST_WORKER_ID).toEqual('3'); -}); - -it('initializes the thread with the given workerPath', () => { +it('initializes the thread with the given workerPath and workerId', () => { const worker = new Worker({ forkOptions: {}, maxRetries: 3, setupArgs: ['foo', 'bar'], + workerId: 2, workerPath: '/tmp/foo/bar/baz.js', }); @@ -109,6 +97,7 @@ it('initializes the thread with the given workerPath', () => { false, '/tmp/foo/bar/baz.js', ['foo', 'bar'], + '3', ]); }); diff --git a/packages/jest-worker/src/workers/__tests__/threadChild.test.js b/packages/jest-worker/src/workers/__tests__/threadChild.test.js index e22a5d679cb5..657ce23a33bb 100644 --- a/packages/jest-worker/src/workers/__tests__/threadChild.test.js +++ b/packages/jest-worker/src/workers/__tests__/threadChild.test.js @@ -128,6 +128,18 @@ afterEach(() => { thread.removeAllListeners('message'); }); +it('sets env.JEST_WORKER_ID', () => { + thread.emit('message', [ + CHILD_MESSAGE_INITIALIZE, + true, // Not really used here, but for flow type purity. + './my-fancy-worker', + [], + '3', + ]); + + expect(process.env.JEST_WORKER_ID).toBe('3'); +}); + it('lazily requires the file', () => { expect(mockCount).toBe(0); diff --git a/packages/jest-worker/src/workers/threadChild.ts b/packages/jest-worker/src/workers/threadChild.ts index c6d423f69543..51b13ca41c21 100644 --- a/packages/jest-worker/src/workers/threadChild.ts +++ b/packages/jest-worker/src/workers/threadChild.ts @@ -41,6 +41,7 @@ const messageListener = (request: any) => { const init: ChildMessageInitialize = request; file = init[2]; setupArgs = request[3]; + process.env.JEST_WORKER_ID = request[4]; break; case CHILD_MESSAGE_CALL: