From 54ce3f3ad11677d40af6091ce70d8afbde8ee2aa Mon Sep 17 00:00:00 2001 From: Tim Seckinger Date: Tue, 26 Mar 2019 11:42:28 +0100 Subject: [PATCH] fix 1-indexed JEST_WORKER_ID (#8205) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Fixes #8204 It's weird that it's 1-indexed (I looked at making our `workerId` 1-indexed too for consistency, but that would make our code really weird), but I guess it's documented, used to work like that and still does for `runInBand` 🤷‍♂️ ## Test plan --- CHANGELOG.md | 1 + packages/jest-worker/src/workers/ChildProcessWorker.ts | 2 +- packages/jest-worker/src/workers/NodeThreadsWorker.ts | 2 +- .../src/workers/__tests__/ChildProcessWorker.test.js | 6 +++--- .../src/workers/__tests__/NodeThreadsWorker.test.js | 4 ++-- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 944c19d56f65..45fab0ee4a6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - `[pretty-format]` Print `BigInt` as a readable number instead of `{}` ([#8138](https://github.com/facebook/jest/pull/8138)) - `[jest-core]` Fix ability to transform dependencies required from globalSetup script [#8143](https://github.com/facebook/jest/pull/8143) - `[@jest/reporters]` Fix Cannot read property converageData of null ([#8168](https://github.com/facebook/jest/pull/8168)) +- `[jest-worker]` `JEST_WORKER_ID` starts at 1 ([#8205](https://github.com/facebook/jest/pull/8205)) ### Chore & Maintenance diff --git a/packages/jest-worker/src/workers/ChildProcessWorker.ts b/packages/jest-worker/src/workers/ChildProcessWorker.ts index 2b540c762c7c..d58197781cca 100644 --- a/packages/jest-worker/src/workers/ChildProcessWorker.ts +++ b/packages/jest-worker/src/workers/ChildProcessWorker.ts @@ -67,7 +67,7 @@ export default class ChildProcessWorker implements WorkerInterface { cwd: process.cwd(), env: { ...process.env, - JEST_WORKER_ID: String(this._options.workerId), + JEST_WORKER_ID: String(this._options.workerId + 1), // 0-indexed workerId, 1-indexed JEST_WORKER_ID ...forceColor, } as NodeJS.ProcessEnv, // Suppress --debug / --inspect flags while preserving others (like --harmony). diff --git a/packages/jest-worker/src/workers/NodeThreadsWorker.ts b/packages/jest-worker/src/workers/NodeThreadsWorker.ts index 45d748e40e8a..b76f1ee89037 100644 --- a/packages/jest-worker/src/workers/NodeThreadsWorker.ts +++ b/packages/jest-worker/src/workers/NodeThreadsWorker.ts @@ -54,7 +54,7 @@ export default class ExperimentalWorker implements WorkerInterface { cwd: process.cwd(), env: { ...process.env, - JEST_WORKER_ID: String(this._options.workerId), + 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)), diff --git a/packages/jest-worker/src/workers/__tests__/ChildProcessWorker.test.js b/packages/jest-worker/src/workers/__tests__/ChildProcessWorker.test.js index 8b1e2fad4c35..cce114d6f991 100644 --- a/packages/jest-worker/src/workers/__tests__/ChildProcessWorker.test.js +++ b/packages/jest-worker/src/workers/__tests__/ChildProcessWorker.test.js @@ -58,7 +58,7 @@ it('passes fork options down to child_process.fork, adding the defaults', () => execPath: 'hello', }, maxRetries: 3, - workerId: process.env.JEST_WORKER_ID, + workerId: process.env.JEST_WORKER_ID - 1, workerPath: '/tmp/foo/bar/baz.js', }); @@ -72,7 +72,7 @@ it('passes fork options down to child_process.fork, adding the defaults', () => }); }); -it('passes workerId to the child process and assign it to env.JEST_WORKER_ID', () => { +it('passes workerId to the child process and assign it to 1-indexed env.JEST_WORKER_ID', () => { new Worker({ forkOptions: {}, maxRetries: 3, @@ -80,7 +80,7 @@ it('passes workerId to the child process and assign it to env.JEST_WORKER_ID', ( workerPath: '/tmp/foo', }); - expect(childProcess.fork.mock.calls[0][2].env.JEST_WORKER_ID).toEqual('2'); + expect(childProcess.fork.mock.calls[0][2].env.JEST_WORKER_ID).toEqual('3'); }); it('initializes the child process with the given workerPath', () => { diff --git a/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js b/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js index 998f12c1f808..d729a7661cc6 100644 --- a/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js +++ b/packages/jest-worker/src/workers/__tests__/NodeThreadsWorker.test.js @@ -63,7 +63,7 @@ it('passes fork options down to child_process.fork, adding the defaults', () => execPath: 'hello', }, maxRetries: 3, - workerId: process.env.JEST_WORKER_ID, + workerId: process.env.JEST_WORKER_ID - 1, workerPath: '/tmp/foo/bar/baz.js', }); @@ -91,7 +91,7 @@ it('passes workerId to the child process and assign it to env.JEST_WORKER_ID', ( }); expect(childProcess.mock.calls[0][1].workerData.env.JEST_WORKER_ID).toEqual( - '2', + '3', ); });