stryker-js icon indicating copy to clipboard operation
stryker-js copied to clipboard

Jest: JEST_WORKER_ID is always 1 even in concurrent mode

Open vostrnad opened this issue 1 year ago • 2 comments

Summary

Jest uses an environment variable named JEST_WORKER_ID that is unique for each worker. This can be used to avoid collisions when using external resources, e.g. file system or external database. However, when using the Stryker Jest Runner, this environment variable is always 1, regardless of the concurrency setting, which potentially breaks any tests relying on it.

Stryker config

{
  "packageManager": "yarn",
  "reporters": ["html", "clear-text", "progress"],
  "testRunner": "jest",
  "coverageAnalysis": "perTest"
}

Stryker environment

+-- @stryker-mutator/[email protected]
+-- @stryker-mutator/[email protected]
+-- [email protected]
+-- [email protected]

jest.config.json:

{
  "preset": "ts-jest",
  "testEnvironment": "node"
}

vostrnad avatar May 14 '23 14:05 vostrnad

Hmm interesting. I don't think we can influence the value of JEST_WORKER_ID inside the testing environment. It is simply always 1, because a runner isn't aware of other runners. Would using process.env.STRYKER_MUTATOR_WORKER ?? process.env.JEST_WORKER_ID be an option for you?

See also https://stryker-mutator.io/docs/stryker-js/parallel-workers/

nicojs avatar May 16 '23 19:05 nicojs

Thanks for the answer, I was not aware of STRYKER_MUTATOR_WORKER but that does solve the issue (it only requires a small update to the test suite). If it's really the case that you have no control over JEST_WORKER_ID, feel free to close this as "not planned".

vostrnad avatar May 16 '23 19:05 vostrnad