Skip to content

@kjanat/dreamcli/testkit ​

Test utilities for running commands in-process.

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

runCommand(command, argv, options?) ​

Run a command in-process and return a RunResult.

ts
import { runCommand } from '@kjanat/dreamcli/testkit';

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 | undefined>Environment variables
configRecord<string, unknown>Config file values
stdinDatastring | nullData to pipe to process stdin for tests
answersunknown[]Prompt answers in order
prompterPromptEngineCustom prompt handler
jsonModebooleanSimulate --json mode
helpHelpOptionsHelp formatting options
verbosityVerbosityOutput verbosity
isTTYbooleanSimulate TTY connection
stat(path) => Promise<'file' | 'directory' | null>Filesystem probe for flag.path() / arg.path() checks
mkdir(path) => Promise<void>Directory creation for create checks
pathChecks'lenient' | 'strict'Fail with PATH_CHECK_SKIPPED when a declared check cannot run (default 'lenient')

RunResult ​

FieldTypeDescription
exitCodenumberProcess exit code
stdoutstring[]Captured stdout lines
stderrstring[]Captured stderr lines
errorCLIError | undefinedStructured error, undefined when none thrown
activityActivityEvent[]Spinner/progress events
skippedPathChecksSkippedPathCheck[]Declared path checks that did not run for lack of stat / mkdir

exitCode can be non-zero while error is undefined when a handler calls out.setExitCode(code) for normal-output status results.

A successful result with entries in skippedPathChecks ran its action without consulting the filesystem, so it does not prove the real CLI accepts the same path. See Filesystem Checks for the probe and strict-mode recipes.

createCaptureOutput() ​

Create an output channel that captures all writes for assertions.

createTestPrompter(answers) ​

Create a prompt engine that returns pre-defined answers.

ts
import { createTestPrompter } from '@kjanat/dreamcli/testkit';

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

createTestAdapter(options?) ​

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

Use terminalSize: { columns, rows } to simulate terminal-aware help wrapping in cli().run({ adapter }) tests.

PROMPT_CANCEL ​

Sentinel value to simulate prompt cancellation.

ts
import {
  createTestPrompter,
  PROMPT_CANCEL,
  runCommand,
} from '@kjanat/dreamcli/testkit';

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

Released under the MIT License.