Skip to content

Commit f153127

Browse files
silverwindfisker
andauthored
Add JEST_WORKER_ID to runner environment (#58)
Co-authored-by: fisker <lionkay@gmail.com>
1 parent 25861a1 commit f153127

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The lists below are not comprehensive: feel free to [start a discussion](https:/
1616
- Inline and external snapshots
1717
- Jest cli options: `--testNamePattern`/`-t`, `--maxWorkers`, `--runInBand`
1818
- Jest config options: `setupFiles`, `setupFilesAfterEnv`, `snapshotSerializers`, `maxWorkers`, `snapshotFormat`, `snapshotResolver`, `slowTestThreshold`
19+
- Jest environment variables: `JEST_WORKER_ID`
1920

2021
### Unsupported Jest features
2122

src/worker-runner.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { performance } from "perf_hooks";
44
import * as snapshot from "jest-snapshot";
55
import { jestExpect as expect } from "@jest/expect";
66
import * as circus from "jest-circus";
7-
import Tinypool from "tinypool";
7+
import Tinypool, { workerId } from "tinypool";
88

99
/** @typedef {{ failures: number, passes: number, pending: number, start: number, end: number }} Stats */
1010
/** @typedef {{ ancestors: string[], title: string, duration: number, errors: Error[], skipped: boolean }} InternalTestResult */
@@ -15,12 +15,15 @@ const initialSetup = once(async projectConfig => {
1515
// We can "polyfill" it for process.cwd() usage, but it
1616
// won't affect path.* and fs.* functions.
1717
if (Tinypool.isWorkerThread) {
18+
process.env.JEST_WORKER_ID = String(workerId);
1819
const startCwd = process.cwd();
1920
let cwd = startCwd;
2021
process.cwd = () => cwd;
2122
process.chdir = dir => {
2223
cwd = path.resolve(cwd, dir);
2324
};
25+
} else {
26+
process.env.JEST_WORKER_ID = "1";
2427
}
2528

2629
for (const setupFile of projectConfig.setupFiles) {

0 commit comments

Comments
 (0)