TestTerminal unstable
Provides scripted terminal input and captures displayed text for tests.
Tests queue keys and text before or while a program reads terminal input,
then inspect the displayed text with output. Input is finite by default:
if a prompt still needs input when the script runs out, Prompt.run fails
with Terminal.QuitError.
Pass open: true to keep input open until endInput.
Constructors
Creates a TestTerminal with fixed dimensions and an empty script.
Details
readLine returns the text up to the next enter or return. When input
ends first, it returns the partial line, or fails with Terminal.QuitError
when no text was typed. Escape, Ctrl+C and Ctrl+D fail with Terminal.QuitError,
even after partial input.
See
- layer for providing the terminal as the
Terminalservice
Signature
declare function make(options?: Options): Effect<TestTerminal>Layers
Creates a Layer that provides a fresh TestTerminal as the Terminal
service.
See
- TestTerminal.Options for the terminal options
Signature
declare function layer(options?: Options): Layer<Terminal>Example Feeding input to a running prompt
import { Effect, Fiber } from "effect"import { Prompt } from "effect/cli"import { TestTerminal } from "effect/testing"
const program = Effect.gen(function*() { const fiber = yield* Effect.forkChild(Prompt.run(Prompt.String({ message: "Name" }))) yield* Effect.yieldNow yield* TestTerminal.inputText("Ada\n") return yield* Fiber.join(fiber)}).pipe(Effect.provide(TestTerminal.layer({ open: true })))
await Effect.runPromise(program) // => "Ada"Models
A Terminal that reads scripted input and records displayed text.
Details
Each reader takes the scripted input that is still unread. Input it leaves
unread returns to the script when its scope closes, so one script can feed
several Prompt.run calls in a row. Escape, Ctrl+C and Ctrl+D are
delivered and then end input for the current reader, as NodeTerminal
does.
See
- layer for providing a
TestTerminalto an effect
Signature
interface TestTerminal extends Terminal { readonly endInput: Effect<void>; readonly inputKey: (name: string, modifiers?: Modifiers) => Effect<void>; readonly inputText: (text: string) => Effect<void>; readonly output: Effect<readonly Array<string>>;}Example Scripting a select prompt
import { Effect } from "effect"import { Prompt } from "effect/cli"import { TestTerminal } from "effect/testing"
const program = Effect.gen(function*() { yield* TestTerminal.inputKey("down") yield* TestTerminal.inputKey("enter")
return yield* Prompt.run(Prompt.Select({ message: "Pick", choices: [{ title: "Alpha", value: "alpha" }, { title: "Beta", value: "beta" }] }))}).pipe(Effect.provide(TestTerminal.layer()))
await Effect.runPromise(program) // => "beta"Other
TestTerminal
unstableTypes used to configure a TestTerminal.
Testing
Ends input on a TestTerminal created with open: true once the queued
script has been read.
Signature
declare const endInput: Effect.Effect<void, never, Terminal.Terminal>Queues one key event on the provided TestTerminal.
Details
Only single-character names carry input. Escape, Ctrl+C and Ctrl+D end
input for the reader that receives them.
See
- inputText for typing text
Signature
declare function inputKey(name: string, modifiers?: Modifiers): Effect<void, never, Terminal>Queues one key event per character of text on the provided
TestTerminal, with \n as enter and \r as return.
See
- inputKey for named keys and modifiers
Signature
declare function inputText(text: string): Effect<void, never, Terminal>Returns every chunk displayed on the provided TestTerminal, in order.
Signature
declare const output: Effect.Effect<ReadonlyArray<string>, never, Terminal.Terminal>