Skip to content

fix: make qase.* work in Jest worker processes - #1041

Merged
gibiw merged 3 commits into
mainfrom
fix/jest-workers
Oct 30, 2026
Merged

gibiw merged 3 commits into
mainfrom
fix/jest-workers

Conversation

@gibiw

@gibiw gibiw commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Summary

jest-qase-reporter 2.8.0. Fixes #762: with more than one Jest worker (Jest's default) every qase.* call threw TypeError and the test was reported as invalid, because the API lived on global.Qase in the main process while tests run in workers. Only --runInBand worked.

How: tests write each test's metadata to a temp directory the reporter creates (passed to workers via QASE_JEST_METADATA_DIR); the reporter reads it in onTestCaseResult. Writes go through a rename (no torn reads); a bad file never stops the run; the directory is removed on exit (not on run completion, so watch mode keeps working).

The current test is resolved from Jest's own state:

  • test.concurrent via currentConcurrentTestName (as jest-snapshot does); Jest 30 reports sequential tests there too, handled.
  • retries keyed by attempt (invocations), so each reported attempt has its own metadata.
  • beforeAll/afterAll calls are dropped instead of landing on the previous test (circus keeps currentTestName after a test ends, and skipped/todo tests never finish).

Also: qase.* without the reporter configured is a no-op instead of throwing; examples no longer force --runInBand; docs say the network profiler still needs a single process; unused env.ts removed.

Test plan

  • Repro on old code: 30 files, -w=3 → 181 problems across 210 results; new code → 0
  • Generated suites with file-unique metadata for every qase.* call, checked for cross-test leakage: 30/60 files, 1–8 workers and --runInBand, on Jest 29.7 and Jest 30.5
  • Stress: 20 runs × Jest 29 + 20 runs × Jest 30 (30 files, varying workers) — 0 problems, 0 leftover temp dirs
  • Edge cases: same test name in different files, beforeEach, beforeAll/afterAll (incl. after skip/todo), test.each, test.concurrent, jest.retryTimes, no reporter configured
  • examples/single/jest run exactly as CI does, without --runInBand and with a fresh cache → matches expected/jest-examples.yaml (no baseline change)
  • Unit tests: 86 pass, including channel tests running inside real jest-circus (hooks, concurrent, retries)

gibiw added 2 commits October 30, 2026 16:49
The reporter exposed qase.* through global.Qase in the main process,
while Jest runs tests in worker processes, so with more than one worker
every call threw TypeError and the test was reported as invalid. Only
--runInBand worked (#762).

Tests now write each test's metadata to a temporary directory the
reporter creates (passed to workers through QASE_JEST_METADATA_DIR) and
the reporter reads it when the test's result arrives. Files are written
through a rename so a result never sees a half-written file, and a bad
file never stops the run.

The current test is resolved from Jest's own state:
- test.concurrent through currentConcurrentTestName, as jest-snapshot
  does; Jest 30 reports sequential tests there too.
- retries are keyed by attempt, so each reported attempt gets its own
  metadata.
- calls from beforeAll/afterAll are dropped instead of landing on the
  previous test, including after skipped and todo tests.

Calls without the reporter configured are no-ops instead of throwing.
Examples no longer force --runInBand; docs note that the network
profiler still needs a single process. Removes the unused env.ts.
- With injectGlobals: false there is no global expect; fall back to
  @jest/globals, which Jest's runtime serves to every test file. Every
  qase.* call was silently dropped in that setup.
- Jest passes the runner's shared context to onTestCaseResult, so a
  per-test result does not say which project ran it, and a file run by
  several projects at once mixed their metadata. The reporter now tags
  each project's globals with its id (read by the worker), tracks which
  projects run each file, and reports overlapping runs from onTestResult,
  which carries the real project. The tag is never overwritten, since
  projects without their own globals share Jest's default object.
@gibiw

gibiw commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up after a second verification round (commit 0b3a09a):

  • injectGlobals: false: every qase.* call was silently dropped (no global expect). Now falls back to @jest/globals, which jest-runtime serves itself. Verified node/jsdom × injectGlobals true/false × workers/in-band.
  • Jest projects running the same file at once: metadata mixed between projects (~1 in 10 runs). Root cause: Jest passes the runner's shared context to onTestCaseResult, so a per-test result doesn't identify its project. The reporter now tags each project's globals with its id (read by workers), tracks which projects run each file, and reports overlapping runs from onTestResult, which carries the real project. Verified 0 problems over 30+ runs on Jest 29/30, including projects with only globals: {}.
  • Known limitation (documented): projects that define no globals share Jest's default object and can't be told apart; their overlapping runs of one file can still mix metadata (1/10 runs in the stress test).

Full matrix re-run on the final build: generated suites 1–8 workers + in-band on Jest 29/30, stress, retries, no-reporter, environments, projects, and examples/single/jest vs expected/jest-examples.yaml — all green, no leftover temp dirs.

The worker support from this PR was only documented in docs/usage.md and the changelog.
@gibiw
gibiw merged commit 1acdb20 into main Oct 30, 2026
56 checks passed
@gibiw
gibiw deleted the fix/jest-workers branch October 30, 2026 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot read properties of undefined (reading 'suite')

2 participants