@hughescr/stryker-bun-runner 1.3.1 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,9 +11,11 @@ import {
11
11
  initializeStrykerNamespace,
12
12
  setActiveMutant,
13
13
  formatCoverageData,
14
+ detectGapWindowBleed,
14
15
  writeCoverageToFile,
15
16
  startOrphanWatchdog,
16
- type StrykerNamespace
17
+ type StrykerNamespace,
18
+ type LateHitEntry
17
19
  } from '__PRELOAD_LOGIC_PATH__';
18
20
 
19
21
  // ============================================================================
@@ -101,6 +103,24 @@ function extractFilePrefix(bunMain: string): string {
101
103
  return sandboxMatch ? sandboxMatch[1] : bunMain.replace(/^.*\//, '');
102
104
  }
103
105
 
106
+ // ============================================================================
107
+ // Cross-test async coverage-bleed detection (gap-window count-delta)
108
+ // ============================================================================
109
+ // Detects fire-and-forget promise chains (or other async work) that keep
110
+ // recording mutant coverage into the `static` bucket after their originating
111
+ // test's afterEach has fired — i.e. between one test ending and the next
112
+ // test beginning ("the gap window"). Diagnostic only: see
113
+ // emitCoverageBleedWarnings in bun-test-runner.ts for how this surfaces as a
114
+ // warning; it never changes coverage attribution.
115
+ //
116
+ // lastEndedTestId / lastFilePrefix / staticSnapshotAtLastBoundary are updated
117
+ // in afterEach (the start of a gap window) and consumed in the NEXT
118
+ // beforeEach (the end of that gap window) — see both hooks below.
119
+ let lastEndedTestId: string | undefined;
120
+ let lastFilePrefix: string | undefined;
121
+ let staticSnapshotAtLastBoundary = new Map<string, number>();
122
+ const lateHits: LateHitEntry[] = [];
123
+
104
124
  if(syncPort && shouldCollectCoverage) {
105
125
  try {
106
126
  // Install the real 'ready' handler before yielding to the event loop so
@@ -210,7 +230,7 @@ const writeCoverageData = () => {
210
230
  // counterToName is not populated (test names are resolved by coverage-mapper
211
231
  // from the inspector data, not stored here), so pass an empty Map.
212
232
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access -- Placeholder import replaced at runtime
213
- const data = formatCoverageData(strykerGlobal.mutantCoverage, new Map<string, string>());
233
+ const data = formatCoverageData(strykerGlobal.mutantCoverage, new Map<string, string>(), lateHits);
214
234
 
215
235
  try {
216
236
  // eslint-disable-next-line @typescript-eslint/no-unsafe-call -- Placeholder import replaced at runtime
@@ -241,6 +261,29 @@ if(shouldCollectCoverage) {
241
261
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- Bun global accessed at runtime
242
262
  const bunMain = String((globalThis as unknown as { Bun?: { main?: string } }).Bun?.main ?? '');
243
263
  const filePrefix = extractFilePrefix(bunMain);
264
+
265
+ // Gap-window bleed check: this beforeEach is the END of the gap window that
266
+ // started when the previous test's afterEach ran (see afterEach below). Diff
267
+ // the static bucket now against the snapshot taken back then; any mutant whose
268
+ // count increased executed while no test was active — i.e. after the previous
269
+ // test ended.
270
+ if(filePrefix !== lastFilePrefix) {
271
+ // FILE-BOUNDARY SKIP: this test starts a new file relative to the last one
272
+ // that ended. A new file's module-level code (imports, describe-level setup)
273
+ // legitimately executes in this gap and would be indistinguishable from real
274
+ // bleed from here, so we deliberately do not diff across a file boundary.
275
+ // This trades missed cross-file leaks for zero file-boundary false positives,
276
+ // which is the safer default for a diagnostic-only warning.
277
+ lastEndedTestId = undefined;
278
+ staticSnapshotAtLastBoundary = new Map();
279
+ } else if(lastEndedTestId !== undefined) {
280
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument -- Placeholder import replaced at runtime
281
+ const bledMutantIds = detectGapWindowBleed(staticSnapshotAtLastBoundary, mutantCoverage.static);
282
+ if(bledMutantIds.length > 0) {
283
+ lateHits.push({ testId: lastEndedTestId, mutantIds: bledMutantIds });
284
+ }
285
+ }
286
+
244
287
  const prevCount = perFileCounters.get(filePrefix) ?? 0;
245
288
  const nextCount = prevCount + 1;
246
289
  perFileCounters.set(filePrefix, nextCount);
@@ -252,13 +295,53 @@ if(shouldCollectCoverage) {
252
295
  });
253
296
 
254
297
  afterEach(() => {
255
- // Clear currentTestId so any subsequent code records to static
298
+ // Record the gap-window START: which test just ended, and the static bucket's
299
+ // counts at this instant. The NEXT beforeEach (above) diffs against this
300
+ // snapshot to detect any coverage recorded in between — see
301
+ // "Cross-test async coverage-bleed detection" above.
302
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment -- StrykerGlobal from placeholder import
303
+ const endedTestId: string | undefined = strykerGlobal.currentTestId;
304
+ lastEndedTestId = endedTestId;
305
+ if(endedTestId) {
306
+ const sepIdx = endedTestId.indexOf('@@');
307
+ lastFilePrefix = sepIdx === -1 ? endedTestId : endedTestId.slice(0, sepIdx);
308
+ }
309
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument -- MutantCoverage from placeholder import
310
+ staticSnapshotAtLastBoundary = new Map(Object.entries(mutantCoverage.static));
311
+
312
+ // Clear currentTestId so any subsequent code records to static
256
313
  // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- StrykerGlobal from placeholder import
257
314
  strykerGlobal.currentTestId = undefined;
258
315
  });
259
316
 
260
317
  afterAll(() => {
318
+ // Final gap check, best-effort: catches bleed that has already recorded a
319
+ // counter delta by the time afterAll runs, with no following beforeEach
320
+ // left to catch it any other way. This preload's afterAll fires once per
321
+ // WORKER PROCESS (empirically verified — Bun runs multiple test files
322
+ // sequentially in one worker sharing this preload module instance), so
323
+ // this is the single final opportunity to detect trailing bleed before
324
+ // coverage is written below.
325
+ //
326
+ // LIMITATION: this only sees hits that execute before afterAll itself
327
+ // runs. A queued timer or promise continuation that is still pending
328
+ // when afterAll fires (and settles afterward, e.g. during process
329
+ // teardown) is never observed — there is no later hook to diff against.
330
+ // Fully catching trailing leaks would need a deliberate strict-drain
331
+ // mode (draining the microtask/timer queue before treating the run as
332
+ // over) — deliberately out of scope here (event-loop starvation risk);
333
+ // see README's "Coverage-bleed warning" section.
334
+ if(lastEndedTestId !== undefined) {
335
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument -- Placeholder import replaced at runtime
336
+ const bledMutantIds = detectGapWindowBleed(staticSnapshotAtLastBoundary, mutantCoverage.static);
337
+ if(bledMutantIds.length > 0) {
338
+ lateHits.push({ testId: lastEndedTestId, mutantIds: bledMutantIds });
339
+ }
340
+ }
341
+
261
342
  ws?.close();
262
343
  writeCoverageData();
344
+ // Clear the accumulator now that it has been written out.
345
+ lateHits.length = 0;
263
346
  });
264
347
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hughescr/stryker-bun-runner",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Stryker test runner plugin for Bun with perTest coverage support",
5
5
  "keywords": [
6
6
  "stryker",