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.
This commit is contained in:
parent
70b814a852
commit
165c8122f6
@ -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 { createEventState, processEvents, serializeError, type EventState } from "./events"
|
||||||
import type { RunContext, EventPayload } from "./types"
|
import type { RunContext, EventPayload } from "./types"
|
||||||
|
|
||||||
@ -100,12 +100,21 @@ describe("event handling", () => {
|
|||||||
|
|
||||||
const events = toAsyncIterable([payload])
|
const events = toAsyncIterable([payload])
|
||||||
|
|
||||||
// when
|
const baselineCallCount = errorSpy.mock.calls.length
|
||||||
await processEvents(ctx, events, state)
|
|
||||||
|
|
||||||
// then
|
try {
|
||||||
expect(errorSpy).not.toHaveBeenCalled()
|
// when
|
||||||
errorSpy.mockRestore()
|
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 () => {
|
it("logs full event traces when verbose is enabled", async () => {
|
||||||
@ -121,14 +130,21 @@ describe("event handling", () => {
|
|||||||
|
|
||||||
const events = toAsyncIterable([payload])
|
const events = toAsyncIterable([payload])
|
||||||
|
|
||||||
// when
|
const baselineCallCount = errorSpy.mock.calls.length
|
||||||
await processEvents(ctx, events, state)
|
|
||||||
|
|
||||||
// then
|
try {
|
||||||
expect(errorSpy).toHaveBeenCalledTimes(1)
|
// when
|
||||||
const firstCall = errorSpy.mock.calls[0]
|
await processEvents(ctx, events, state)
|
||||||
expect(String(firstCall?.[0] ?? "")).toContain("custom.event")
|
|
||||||
errorSpy.mockRestore()
|
// 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 () => {
|
it("session.idle sets mainSessionIdle to true for matching session", async () => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user