From f3cd7f566f7ddb2dbd12bff109f8e849e5bfa6bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sat, 11 Dec 2021 16:23:00 +0000 Subject: [PATCH 1/3] [jest-worker] Initialize `JEST_WORKER_ID` using `CHILD_MESSAGE_INITIALIZE` with `worker_threads` --- .../src/workers/NodeThreadsWorker.ts | 5 +---- .../workers/__tests__/NodeThreadsWorker.test.js | 17 +++-------------- .../src/workers/__tests__/threadChild.test.js | 12 ++++++++++++ packages/jest-worker/src/workers/threadChild.ts | 1 + 4 files changed, 17 insertions(+), 18 deletions(-) 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..5a3bdfd38617 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,24 +83,13 @@ 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'], workerPath: '/tmp/foo/bar/baz.js', + workerId: 2, }); expect(worker._worker.postMessage.mock.calls[0][0]).toEqual([ @@ -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..d298b38f4f0f 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: From 7d8b833c7e73152219c37630cc89b180666ea8ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sat, 11 Dec 2021 18:03:38 +0100 Subject: [PATCH 2/3] Lint --- .../src/workers/__tests__/NodeThreadsWorker.test.js | 4 ++-- .../jest-worker/src/workers/__tests__/threadChild.test.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js b/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js index 5a3bdfd38617..131ee206ad0d 100644 --- a/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js +++ b/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js @@ -88,8 +88,8 @@ it('initializes the thread with the given workerPath and workerId', () => { forkOptions: {}, maxRetries: 3, setupArgs: ['foo', 'bar'], - workerPath: '/tmp/foo/bar/baz.js', workerId: 2, + workerPath: '/tmp/foo/bar/baz.js', }); expect(worker._worker.postMessage.mock.calls[0][0]).toEqual([ @@ -97,7 +97,7 @@ it('initializes the thread with the given workerPath and workerId', () => { false, '/tmp/foo/bar/baz.js', ['foo', 'bar'], - '3' + '3', ]); }); diff --git a/packages/jest-worker/src/workers/__tests__/threadChild.test.js b/packages/jest-worker/src/workers/__tests__/threadChild.test.js index d298b38f4f0f..657ce23a33bb 100644 --- a/packages/jest-worker/src/workers/__tests__/threadChild.test.js +++ b/packages/jest-worker/src/workers/__tests__/threadChild.test.js @@ -134,11 +134,11 @@ it('sets env.JEST_WORKER_ID', () => { true, // Not really used here, but for flow type purity. './my-fancy-worker', [], - '3' + '3', ]); expect(process.env.JEST_WORKER_ID).toBe('3'); -}) +}); it('lazily requires the file', () => { expect(mockCount).toBe(0); From 615c301a455e16e1b78a40366198eafd68217b8d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 13 Dec 2021 20:13:48 +0100 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) 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