stryker-mutator / stryker-js

Mutation testing for JavaScript and friends
https://stryker-mutator.io
Apache License 2.0
2.59k stars 251 forks source link

Jest: JEST_WORKER_ID is always 1 even in concurrent mode #4192

Open vostrnad opened 1 year ago

vostrnad commented 1 year ago

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/core@6.4.2
+-- @stryker-mutator/jest-runner@6.4.2
+-- jest@29.5.0
+-- ts-jest@29.1.0

jest.config.json:

{
  "preset": "ts-jest",
  "testEnvironment": "node"
}
nicojs commented 1 year ago

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/

vostrnad commented 1 year ago

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".