test(log): avoid global logger module mocking

Replace the effect-log tests' global util/log module mock with local spies on Log.create so the logger compatibility tests no longer leak into unrelated unit suites. Keep the coverage for routing, annotations, spans, and cause formatting while allowing the full package test workflow to run cleanly.
This commit is contained in:
Kit Langton
2026-03-12 17:00:20 -04:00
parent 9bbc874927
commit e13d44684d

View File

@@ -1,21 +1,25 @@
import { beforeEach, expect, mock, test } from "bun:test" import { afterEach, expect, mock, spyOn, test } from "bun:test"
import { Cause, Effect } from "effect" import { Cause, Effect } from "effect"
import { CurrentLogAnnotations, CurrentLogSpans } from "effect/References" import { CurrentLogAnnotations, CurrentLogSpans } from "effect/References"
import * as EffectLog from "../../src/util/effect-log"
import { Log } from "../../src/util/log"
const debug = mock(() => {}) const debug = mock(() => {})
const info = mock(() => {}) const info = mock(() => {})
const warn = mock(() => {}) const warn = mock(() => {})
const error = mock(() => {}) const error = mock(() => {})
const create = mock(() => ({
const logger = {
debug, debug,
info, info,
warn, warn,
error, error,
tag() { tag() {
return this return logger
}, },
clone() { clone() {
return this return logger
}, },
time() { time() {
return { return {
@@ -23,18 +27,9 @@ const create = mock(() => ({
[Symbol.dispose]() {}, [Symbol.dispose]() {},
} }
}, },
})) }
mock.module("../../src/util/log", () => ({ afterEach(() => {
Log: {
create,
},
}))
const EffectLog = await import("../../src/util/effect-log")
beforeEach(() => {
create.mockClear()
debug.mockClear() debug.mockClear()
info.mockClear() info.mockClear()
warn.mockClear() warn.mockClear()
@@ -42,6 +37,8 @@ beforeEach(() => {
}) })
test("EffectLog.layer routes info logs through util/log", async () => { test("EffectLog.layer routes info logs through util/log", async () => {
using create = spyOn(Log, "create").mockReturnValue(logger)
await Effect.runPromise(Effect.logInfo("hello").pipe(Effect.provide(EffectLog.layer({ service: "effect-test" })))) await Effect.runPromise(Effect.logInfo("hello").pipe(Effect.provide(EffectLog.layer({ service: "effect-test" }))))
expect(create).toHaveBeenCalledWith({ service: "effect-test" }) expect(create).toHaveBeenCalledWith({ service: "effect-test" })
@@ -49,6 +46,8 @@ test("EffectLog.layer routes info logs through util/log", async () => {
}) })
test("EffectLog.layer forwards annotations and spans to util/log", async () => { test("EffectLog.layer forwards annotations and spans to util/log", async () => {
using create = spyOn(Log, "create").mockReturnValue(logger)
await Effect.runPromise( await Effect.runPromise(
Effect.logInfo("hello").pipe( Effect.logInfo("hello").pipe(
Effect.annotateLogs({ requestId: "req-123" }), Effect.annotateLogs({ requestId: "req-123" }),
@@ -57,6 +56,7 @@ test("EffectLog.layer forwards annotations and spans to util/log", async () => {
), ),
) )
expect(create).toHaveBeenCalledWith({ service: "effect-test-meta" })
expect(info).toHaveBeenCalledWith( expect(info).toHaveBeenCalledWith(
"hello", "hello",
expect.objectContaining({ expect.objectContaining({
@@ -71,9 +71,10 @@ test("EffectLog.layer forwards annotations and spans to util/log", async () => {
}) })
test("EffectLog.make formats structured messages and causes for legacy logger", () => { test("EffectLog.make formats structured messages and causes for legacy logger", () => {
const logger = EffectLog.make({ service: "effect-test-struct" }) using create = spyOn(Log, "create").mockReturnValue(logger)
const effect = EffectLog.make({ service: "effect-test-struct" })
logger.log({ effect.log({
message: { hello: "world" }, message: { hello: "world" },
logLevel: "Warn", logLevel: "Warn",
cause: Cause.fail(new Error("boom")), cause: Cause.fail(new Error("boom")),
@@ -88,6 +89,7 @@ test("EffectLog.make formats structured messages and causes for legacy logger",
date: new Date(), date: new Date(),
} as never) } as never)
expect(create).toHaveBeenCalledWith({ service: "effect-test-struct" })
expect(warn).toHaveBeenCalledWith( expect(warn).toHaveBeenCalledWith(
'{"hello":"world"}', '{"hello":"world"}',
expect.objectContaining({ expect.objectContaining({