Skip to content

Commit 8564843

Browse files
authored
Expose captured TestConsole warning, info, debug, and trace output (#10682)
1 parent 80622bf commit 8564843

3 files changed

Lines changed: 154 additions & 9 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Add `TestConsole` readers for captured warning, info, debug, and trace parameters.

‎packages/effect/src/testing/TestConsole.ts‎

Lines changed: 119 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* When the test layer is provided, calls made through the Effect console APIs
55
* are captured in memory instead of being written to the host console. Tests can
66
* then assert on logged values deterministically. This module includes the
7-
* console layer, helpers for reading captured `Console.log` and `Console.error`
8-
* arguments, and access to the provided test console service.
7+
* console layer, readers for captured `Console.log`, `Console.error`, `Console.warn`,
8+
* `Console.info`, `Console.debug`, and `Console.trace` arguments, and access to
9+
* the provided test console service.
910
*
1011
* @stability unstable
1112
* @since 4.0.0
@@ -53,6 +54,10 @@ import * as Layer from "../Layer.ts"
5354
* @see {@link layer} for providing `TestConsole` to an effect
5455
* @see {@link logLines} for reading captured `Console.log` calls
5556
* @see {@link errorLines} for reading captured `Console.error` calls
57+
* @see {@link warnLines} for reading captured `Console.warn` calls
58+
* @see {@link infoLines} for reading captured `Console.info` calls
59+
* @see {@link debugLines} for reading captured `Console.debug` calls
60+
* @see {@link traceLines} for reading captured `Console.trace` calls
5661
*
5762
* @stability unstable
5863
* @category models
@@ -79,6 +84,30 @@ export interface TestConsole extends Console.Console {
7984
* instance.
8085
*/
8186
readonly errorLines: Effect.Effect<ReadonlyArray<unknown>>
87+
/**
88+
* Returns all parameters captured from `Console.warn` calls in call order.
89+
*
90+
* @since 4.1.5
91+
*/
92+
readonly warnLines: Effect.Effect<ReadonlyArray<unknown>>
93+
/**
94+
* Returns all parameters captured from `Console.info` calls in call order.
95+
*
96+
* @since 4.1.5
97+
*/
98+
readonly infoLines: Effect.Effect<ReadonlyArray<unknown>>
99+
/**
100+
* Returns all parameters captured from `Console.debug` calls in call order.
101+
*
102+
* @since 4.1.5
103+
*/
104+
readonly debugLines: Effect.Effect<ReadonlyArray<unknown>>
105+
/**
106+
* Returns all parameters captured from `Console.trace` calls in call order.
107+
*
108+
* @since 4.1.5
109+
*/
110+
readonly traceLines: Effect.Effect<ReadonlyArray<unknown>>
82111
}
83112

84113
/**
@@ -189,11 +218,8 @@ export const make = Effect.gen(function*() {
189218
}
190219
}
191220

192-
const logLines = Effect.sync(() => Array.flatMap(entries, (entry) => entry.method === "log" ? entry.parameters : []))
193-
194-
const errorLines = Effect.sync(() =>
195-
Array.flatMap(entries, (entry) => entry.method === "error" ? entry.parameters : [])
196-
)
221+
const lines = (method: TestConsole.Method) =>
222+
Effect.sync(() => Array.flatMap(entries, (entry) => entry.method === method ? entry.parameters : []))
197223

198224
return {
199225
assert: createEntryUnsafe("assert"),
@@ -215,8 +241,12 @@ export const make = Effect.gen(function*() {
215241
timeLog: createEntryUnsafe("timeLog"),
216242
trace: createEntryUnsafe("trace"),
217243
warn: createEntryUnsafe("warn"),
218-
logLines,
219-
errorLines
244+
logLines: lines("log"),
245+
errorLines: lines("error"),
246+
warnLines: lines("warn"),
247+
infoLines: lines("info"),
248+
debugLines: lines("debug"),
249+
traceLines: lines("trace")
220250
} as TestConsole
221251
})
222252

@@ -376,3 +406,83 @@ export const logLines: Effect.Effect<ReadonlyArray<unknown>, never, never> = tes
376406
export const errorLines: Effect.Effect<ReadonlyArray<unknown>, never, never> = testConsoleWith(
377407
(console) => console.errorLines
378408
)
409+
410+
/**
411+
* Returns all parameters captured from `Console.warn` calls thus far, in call
412+
* order, without formatting them or including calls to other console methods.
413+
*
414+
* **When to use**
415+
*
416+
* Use to assert on captured `Console.warn` output from a program provided with
417+
* `TestConsole.layer`.
418+
*
419+
* @see {@link TestConsole} for the readers of other console methods
420+
* @see {@link layer} for capturing console calls during a test
421+
*
422+
* @stability unstable
423+
* @category testing
424+
* @since 4.1.5
425+
*/
426+
export const warnLines: Effect.Effect<ReadonlyArray<unknown>, never, never> = testConsoleWith(
427+
(console) => console.warnLines
428+
)
429+
430+
/**
431+
* Returns all parameters captured from `Console.info` calls thus far, in call
432+
* order, without formatting them or including calls to other console methods.
433+
*
434+
* **When to use**
435+
*
436+
* Use to assert on captured `Console.info` output from a program provided with
437+
* `TestConsole.layer`.
438+
*
439+
* @see {@link TestConsole} for the readers of other console methods
440+
* @see {@link layer} for capturing console calls during a test
441+
*
442+
* @stability unstable
443+
* @category testing
444+
* @since 4.1.5
445+
*/
446+
export const infoLines: Effect.Effect<ReadonlyArray<unknown>, never, never> = testConsoleWith(
447+
(console) => console.infoLines
448+
)
449+
450+
/**
451+
* Returns all parameters captured from `Console.debug` calls thus far, in call
452+
* order, without formatting them or including calls to other console methods.
453+
*
454+
* **When to use**
455+
*
456+
* Use to assert on captured `Console.debug` output from a program provided with
457+
* `TestConsole.layer`.
458+
*
459+
* @see {@link TestConsole} for the readers of other console methods
460+
* @see {@link layer} for capturing console calls during a test
461+
*
462+
* @stability unstable
463+
* @category testing
464+
* @since 4.1.5
465+
*/
466+
export const debugLines: Effect.Effect<ReadonlyArray<unknown>, never, never> = testConsoleWith(
467+
(console) => console.debugLines
468+
)
469+
470+
/**
471+
* Returns all parameters captured from `Console.trace` calls thus far, in call
472+
* order, without formatting them or including calls to other console methods.
473+
*
474+
* **When to use**
475+
*
476+
* Use to assert on captured `Console.trace` output from a program provided with
477+
* `TestConsole.layer`.
478+
*
479+
* @see {@link TestConsole} for the readers of other console methods
480+
* @see {@link layer} for capturing console calls during a test
481+
*
482+
* @stability unstable
483+
* @category testing
484+
* @since 4.1.5
485+
*/
486+
export const traceLines: Effect.Effect<ReadonlyArray<unknown>, never, never> = testConsoleWith(
487+
(console) => console.traceLines
488+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { assert, describe, it } from "@effect/vitest"
2+
import * as Console from "effect/Console"
3+
import * as Effect from "effect/Effect"
4+
import * as TestConsole from "effect/testing/TestConsole"
5+
6+
describe("TestConsole", () => {
7+
for (
8+
const [method, write, read] of [
9+
["warn", Console.warn, TestConsole.warnLines],
10+
["info", Console.info, TestConsole.infoLines],
11+
["debug", Console.debug, TestConsole.debugLines],
12+
["trace", Console.trace, TestConsole.traceLines]
13+
] as const
14+
) {
15+
it.effect(`${method}Lines captures parameters from only ${method} calls`, () =>
16+
Effect.gen(function*() {
17+
const details = { value: 1 }
18+
yield* Console.log("log", details)
19+
yield* Console.error("error", details)
20+
yield* Console.warn("warn", details)
21+
yield* Console.info("info", details)
22+
yield* Console.debug("debug", details)
23+
yield* Console.trace("trace", details)
24+
yield* write("second", 42)
25+
26+
const lines = yield* read
27+
assert.deepStrictEqual(lines, [method, details, "second", 42])
28+
}))
29+
}
30+
})

0 commit comments

Comments
 (0)