From 165c8122f6d172d72c271978e28cefef5736a2e6 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Sat, 21 Feb 2026 05:08:58 +0900 Subject: [PATCH] test(events): use baseline snapshot pattern for console spy isolation Replace exact call count assertions with delta-based checks: - capture errorSpy.mock.calls.length before processing events - slice to only check calls made during this test's execution - use try/finally to guarantee mockRestore() even on assertion failure This prevents test pollution from cross-file spy leakage in CI batch runs. --- src/cli/run/events.test.ts | 42 ++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/cli/run/events.test.ts b/src/cli/run/events.test.ts index 334d518ef..2afc216f8 100644 --- a/src/cli/run/events.test.ts +++ b/src/cli/run/events.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, spyOn } from "bun:test" +import { afterEach, beforeEach, describe, it, expect, spyOn } from "bun:test" import { createEventState, processEvents, serializeError, type EventState } from "./events" import type { RunContext, EventPayload } from "./types" @@ -100,12 +100,21 @@ describe("event handling", () => { const events = toAsyncIterable([payload]) - // when - await processEvents(ctx, events, state) + const baselineCallCount = errorSpy.mock.calls.length - // then - expect(errorSpy).not.toHaveBeenCalled() - errorSpy.mockRestore() + try { + // when + await processEvents(ctx, events, state) + + // then + const newCalls = errorSpy.mock.calls.slice(baselineCallCount) + const hasEventTrace = newCalls.some((call) => + String(call?.[0] ?? "").includes("custom.event"), + ) + expect(hasEventTrace).toBe(false) + } finally { + errorSpy.mockRestore() + } }) it("logs full event traces when verbose is enabled", async () => { @@ -121,14 +130,21 @@ describe("event handling", () => { const events = toAsyncIterable([payload]) - // when - await processEvents(ctx, events, state) + const baselineCallCount = errorSpy.mock.calls.length - // then - expect(errorSpy).toHaveBeenCalledTimes(1) - const firstCall = errorSpy.mock.calls[0] - expect(String(firstCall?.[0] ?? "")).toContain("custom.event") - errorSpy.mockRestore() + try { + // when + await processEvents(ctx, events, state) + + // then + const newCalls = errorSpy.mock.calls.slice(baselineCallCount) + const hasEventTrace = newCalls.some((call) => + String(call?.[0] ?? "").includes("custom.event"), + ) + expect(hasEventTrace).toBe(true) + } finally { + errorSpy.mockRestore() + } }) it("session.idle sets mainSessionIdle to true for matching session", async () => {