Skip to content

dreamcli/testkit

Test utilities for running commands in-process.

ts
import {
	runCommand,
	createCaptureOutput,
	createTestPrompter,
	createTestAdapter,
	PROMPT_CANCEL,
} from 'dreamcli/testkit';

runCommand(command, argv, options?)

Run a command in-process and return a RunResult.

ts
const result = await runCommand(greet, ['Alice', '--loud']);

Parameters

ParameterTypeDescription
commandCommandBuilderThe command to run
argvstring[]Simulated command-line arguments
optionsRunOptionsOptional configuration

RunOptions

OptionTypeDescription
envRecord<string, string>Environment variables
configRecord<string, unknown>Config file values
answersunknown[]Prompt answers in order
prompterPromptEngineCustom prompt handler
jsonbooleanSimulate --json mode
helpbooleanSimulate --help
verbosityVerbosityOutput verbosity
adapterRuntimeAdapterCustom runtime adapter

RunResult

FieldTypeDescription
exitCodenumberProcess exit code
stdoutstring[]Captured stdout lines
stderrstring[]Captured stderr lines
errorError | undefinedError if action threw
activityActivityEvent[]Spinner/progress events

createCaptureOutput()

Create an output channel that captures all writes for assertions.

createTestPrompter(answers)

Create a prompt engine that returns pre-defined answers.

ts
const prompter = createTestPrompter(['eu', true, 'my-name']);

createTestAdapter(options?)

Create a runtime adapter for testing (no real process access).

PROMPT_CANCEL

Sentinel value to simulate prompt cancellation.

ts
const result = await runCommand(cmd, [], {
	prompter: createTestPrompter([PROMPT_CANCEL]),
});

Released under the MIT License.