TestConsole unstable
Provides a test implementation of the Effect Console service.
When the test layer is provided, calls made through the Effect console APIs
are captured in memory instead of being written to the host console. Tests can
then assert on logged values deterministically. This module includes the
console layer, readers for captured Console.log, Console.error, Console.warn,
Console.info, Console.debug, and Console.trace arguments, and access to
the provided test console service.
Constructors
Creates a new TestConsole instance that captures all console output. The returned TestConsole implements the Console interface and provides additional methods to retrieve logged messages.
When to use
Use to construct a test console service value directly.
See
- layer for providing a
TestConsoleas aLayer
Signature
declare const make: Effect<TestConsole, never, never>Example Creating a test console
import { Console, Effect } from "effect"import { TestConsole } from "effect/testing"
const program = Effect.gen(function*() { yield* Console.log("Debug message") yield* Console.error("Error occurred")
const logs = yield* TestConsole.logLines const errors = yield* TestConsole.errorLines
logs // => ["Debug message"] errors // => ["Error occurred"]}).pipe(Effect.provide(TestConsole.layer))
await Effect.runPromise(program)Layers
Creates a Layer which constructs a TestConsole.
This layer can be used to provide a TestConsole implementation
for testing purposes.
When to use
Use to run an effect with console calls captured by TestConsole.
See
- make for constructing the service value directly
- testConsoleWith for accessing the provided test console service
Signature
declare const layer: Layer.Layer<TestConsole>Example Providing a test console layer
import { Console, Effect } from "effect"import { TestConsole } from "effect/testing"
const program = Effect.gen(function*() { yield* Console.log("This will be captured") yield* Console.error("This error will be captured")
const logs = yield* TestConsole.logLines const errors = yield* TestConsole.errorLines
logs // => ["This will be captured"] errors // => ["This error will be captured"]}).pipe(Effect.provide(TestConsole.layer))
await Effect.runPromise(program)Models
A TestConsole provides a testable implementation of the Console interface.
It captures all console output for testing purposes while maintaining full
compatibility with the standard Console API.
When to use
Use to provide a console implementation that records calls for assertions in tests.
Details
This interface extends the standard Console interface and adds methods to retrieve logged messages for verification in tests.
See
- layer for providing
TestConsoleto an effect - logLines for reading captured
Console.logcalls - errorLines for reading captured
Console.errorcalls - warnLines for reading captured
Console.warncalls - infoLines for reading captured
Console.infocalls - debugLines for reading captured
Console.debugcalls - traceLines for reading captured
Console.tracecalls
Signature
interface TestConsole extends Console { readonly debugLines: Effect<readonly Array<unknown>>; readonly errorLines: Effect<readonly Array<unknown>>; readonly infoLines: Effect<readonly Array<unknown>>; readonly logLines: Effect<readonly Array<unknown>>; readonly traceLines: Effect<readonly Array<unknown>>; readonly warnLines: Effect<readonly Array<unknown>>;}Example Capturing console output in tests
import { Console, Effect } from "effect"import { TestConsole } from "effect/testing"
const program = Effect.gen(function*() { yield* Console.log("Hello, World!") yield* Console.error("An error occurred")
const logs = yield* TestConsole.logLines const errors = yield* TestConsole.errorLines
logs // => ["Hello, World!"] errors // => ["An error occurred"]}).pipe(Effect.provide(TestConsole.layer))
await Effect.runPromise(program)Other
TestConsole
unstableThe TestConsole namespace provides types and utilities for working with
test console implementations.
When to use
Use when referring to types nested under the TestConsole namespace.
Testing
debugLines
unstableReturns all parameters captured from Console.debug calls thus far, in call
order, without formatting them or including calls to other console methods.
When to use
Use to assert on captured Console.debug output from a program provided with
TestConsole.layer.
See
- TestConsole for the readers of other console methods
- layer for capturing console calls during a test
Signature
declare const debugLines: Effect.Effect<ReadonlyArray<unknown>, never, never>errorLines
unstableReturns an array of all items that have been logged by the program using
Console.error thus far.
When to use
Use to assert on captured Console.error output from a program provided
with TestConsole.layer.
See
Signature
declare const errorLines: Effect.Effect<ReadonlyArray<unknown>, never, never>Example Reading captured error lines
import { Console, Effect } from "effect"import { TestConsole } from "effect/testing"
const program = Effect.gen(function*() { yield* Console.error("Error message") yield* Console.error("Another error", new Error("Something went wrong"))
const errors = yield* TestConsole.errorLines
const messages = [errors[0], errors[1], errors[2] instanceof Error ? errors[2].message : undefined] messages // => ["Error message", "Another error", "Something went wrong"]}).pipe(Effect.provide(TestConsole.layer))
await Effect.runPromise(program)Returns all parameters captured from Console.info calls thus far, in call
order, without formatting them or including calls to other console methods.
When to use
Use to assert on captured Console.info output from a program provided with
TestConsole.layer.
See
- TestConsole for the readers of other console methods
- layer for capturing console calls during a test
Signature
declare const infoLines: Effect.Effect<ReadonlyArray<unknown>, never, never>Returns an array of all items that have been logged by the program using
Console.log thus far.
When to use
Use to assert on captured Console.log output from a program provided with
TestConsole.layer.
See
- errorLines for reading captured
Console.erroroutput - layer for capturing console calls during a test
Signature
declare const logLines: Effect.Effect<ReadonlyArray<unknown>, never, never>Example Reading captured log lines
import { Console, Effect } from "effect"import { TestConsole } from "effect/testing"
const program = Effect.gen(function*() { yield* Console.log("First message") yield* Console.log("Second message", { key: "value" }) yield* Console.log("Third message", 42, true)
const logs = yield* TestConsole.logLines
logs // => ["First message", "Second message", { key: "value" }, "Third message", 42, true]}).pipe(Effect.provide(TestConsole.layer))
await Effect.runPromise(program)testConsoleWith
unstableRetrieves the TestConsole service for this test and uses it to run the
specified workflow.
When to use
Use to access the provided test console service inside an effect.
See
- layer for providing the test console service
- logLines for reading captured
Console.logcalls directly - errorLines for reading captured
Console.errorcalls directly
Signature
declare function testConsoleWith<A, E, R>(f: (console: TestConsole) => Effect<A, E, R>): Effect<A, E, R>Example Accessing the test console service
import { Effect } from "effect"import { TestConsole } from "effect/testing"
const program = TestConsole.testConsoleWith((testConsole) => Effect.gen(function*() { testConsole.log("Test message") testConsole.error("Test error")
const logs = yield* testConsole.logLines const errors = yield* testConsole.errorLines
logs // => ["Test message"] errors // => ["Test error"] })).pipe(Effect.provide(TestConsole.layer))
await Effect.runPromise(program)traceLines
unstableReturns all parameters captured from Console.trace calls thus far, in call
order, without formatting them or including calls to other console methods.
When to use
Use to assert on captured Console.trace output from a program provided with
TestConsole.layer.
See
- TestConsole for the readers of other console methods
- layer for capturing console calls during a test
Signature
declare const traceLines: Effect.Effect<ReadonlyArray<unknown>, never, never>Returns all parameters captured from Console.warn calls thus far, in call
order, without formatting them or including calls to other console methods.
When to use
Use to assert on captured Console.warn output from a program provided with
TestConsole.layer.
See
- TestConsole for the readers of other console methods
- layer for capturing console calls during a test
Signature
declare const warnLines: Effect.Effect<ReadonlyArray<unknown>, never, never>