Skip to content

Commit 54ce3f3

Browse files
authored
fix 1-indexed JEST_WORKER_ID (#8205)
<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. The two fields below are mandatory. --> <!-- Please remember to update CHANGELOG.md in the root of the project if you have not done so. --> ## 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` 🤷‍♂️ <!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? --> ## Test plan <!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes UI. -->
1 parent 5816a57 commit 54ce3f3

File tree

5 files changed

+8
-7
lines changed

5 files changed

+8
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- `[pretty-format]` Print `BigInt` as a readable number instead of `{}` ([#8138](https://github.com/facebook/jest/pull/8138))
2121
- `[jest-core]` Fix ability to transform dependencies required from globalSetup script [#8143](https://github.com/facebook/jest/pull/8143)
2222
- `[@jest/reporters]` Fix Cannot read property converageData of null ([#8168](https://github.com/facebook/jest/pull/8168))
23+
- `[jest-worker]` `JEST_WORKER_ID` starts at 1 ([#8205](https://github.com/facebook/jest/pull/8205))
2324

2425
### Chore & Maintenance
2526

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default class ChildProcessWorker implements WorkerInterface {
6767
cwd: process.cwd(),
6868
env: {
6969
...process.env,
70-
JEST_WORKER_ID: String(this._options.workerId),
70+
JEST_WORKER_ID: String(this._options.workerId + 1), // 0-indexed workerId, 1-indexed JEST_WORKER_ID
7171
...forceColor,
7272
} as NodeJS.ProcessEnv,
7373
// Suppress --debug / --inspect flags while preserving others (like --harmony).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default class ExperimentalWorker implements WorkerInterface {
5454
cwd: process.cwd(),
5555
env: {
5656
...process.env,
57-
JEST_WORKER_ID: String(this._options.workerId),
57+
JEST_WORKER_ID: String(this._options.workerId + 1), // 0-indexed workerId, 1-indexed JEST_WORKER_ID
5858
} as NodeJS.ProcessEnv,
5959
// Suppress --debug / --inspect flags while preserving others (like --harmony).
6060
execArgv: process.execArgv.filter(v => !/^--(debug|inspect)/.test(v)),

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ it('passes fork options down to child_process.fork, adding the defaults', () =>
5858
execPath: 'hello',
5959
},
6060
maxRetries: 3,
61-
workerId: process.env.JEST_WORKER_ID,
61+
workerId: process.env.JEST_WORKER_ID - 1,
6262
workerPath: '/tmp/foo/bar/baz.js',
6363
});
6464

@@ -72,15 +72,15 @@ it('passes fork options down to child_process.fork, adding the defaults', () =>
7272
});
7373
});
7474

75-
it('passes workerId to the child process and assign it to env.JEST_WORKER_ID', () => {
75+
it('passes workerId to the child process and assign it to 1-indexed env.JEST_WORKER_ID', () => {
7676
new Worker({
7777
forkOptions: {},
7878
maxRetries: 3,
7979
workerId: 2,
8080
workerPath: '/tmp/foo',
8181
});
8282

83-
expect(childProcess.fork.mock.calls[0][2].env.JEST_WORKER_ID).toEqual('2');
83+
expect(childProcess.fork.mock.calls[0][2].env.JEST_WORKER_ID).toEqual('3');
8484
});
8585

8686
it('initializes the child process with the given workerPath', () => {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ it('passes fork options down to child_process.fork, adding the defaults', () =>
6363
execPath: 'hello',
6464
},
6565
maxRetries: 3,
66-
workerId: process.env.JEST_WORKER_ID,
66+
workerId: process.env.JEST_WORKER_ID - 1,
6767
workerPath: '/tmp/foo/bar/baz.js',
6868
});
6969

@@ -91,7 +91,7 @@ it('passes workerId to the child process and assign it to env.JEST_WORKER_ID', (
9191
});
9292

9393
expect(childProcess.mock.calls[0][1].workerData.env.JEST_WORKER_ID).toEqual(
94-
'2',
94+
'3',
9595
);
9696
});
9797

0 commit comments

Comments
 (0)