Testing Your CLI
maltty ships test utilities at maltty/test so you can test commands, middleware, and full CLI pipelines without mocking @clack/prompts or wiring up streams by hand.
Install
The utilities live inside maltty -- no extra package required:
Unit Testing Commands
Use runHandler to execute a single command handler in isolation:
Unit Testing Middleware
Use runMiddleware to execute a middleware chain with a test context:
Testing with Prompts
Use mockPrompts to pre-program prompt responses without touching @clack/prompts:
mockPrompts accepts queues for each prompt type. Responses are consumed in order:
Building a Test Context Manually
Use createTestContext when you need a context without running a handler:
Integration Testing
Use runCommand to test the full CLI pipeline including arg parsing, middleware, and handlers:
Testing with Auth
Commands that depend on ctx.auth need the auth context decorated before the handler runs. Use createTestContext and decorateContext to attach a mock auth object:
Testing with Config
Commands that use ctx.config expect a ConfigHandle with a load() method. Use createTestContext and decorateContext to attach a mock config handle:
Testing Icons
Commands that use ctx.icons need the icons context decorated. Use createTestContext and decorateContext to attach a mock:
Test Lifecycle
Use setupTestLifecycle to automatically save/restore process.argv and stub process.exit between tests:
API Reference
Troubleshooting
Handler throws unexpectedly
Issue: runHandler rejects instead of returning an error.
Fix: runHandler catches ContextError (from ctx.fail()) and returns it as error. Other exceptions propagate as rejections. Ensure your handler uses ctx.fail() for expected errors.
Prompts consumed out of order
Issue: mockPrompts returns the wrong response.
Fix: Responses are consumed in call order per type. If your handler calls confirm twice, queue two values: mockPrompts({ confirm: [true, false] }).