TestHarness

class
class TestHarness<T>

Utility for testing CLI instances. Can check argument parsing and validation, including
command chain resolution.

Methods

parse(args: string[]): Promise<TestHarnessParseResult<T>>
clearMockedContexts(): void

Removes all mocked contexts registered via mockContext and clears the `lifetime: 'global'` provider cache so the next test starts with a fresh slate. Typically called from `afterEach`: ```ts afterEach(() => TestHarness.clearMockedContexts()); ```

mockContext(_cli: CLI<TArgs, any, any, any, TProviders>, options: MockContextOptions<TArgs, TProviders>): () => void

Mocks the CLI context for testing DI providers and command context outside of a real `forge()` execution. Returns a cleanup function that removes the mocked context when called. `options.providers` accepts both eager values and the same `{ factory, lifetime }` configs that `.provide()` takes — factory mocks run lazily via `inject()` and receive the mocked `args`. This helper is best suited for simple synchronous tests. It does not maintain a stack of prior contexts: calling `mockContext()` while another mock is active overwrites the current store, and the returned cleanup function blanks the store rather than restoring whatever was there before. For tests that use `await` across the mocked block or need nested mocked contexts that unwind properly, prefer runWithMockedContext, which uses `AsyncLocalStorage.run()` for proper scoping.

resetGlobalProviders(): void

Clears only the `lifetime: 'global'` provider cache without touching any active mocked contexts. Useful when a specific test needs to re-initialize global providers without discarding the surrounding mock.

runWithMockedContext(_cli: CLI<TArgs, any, any, any, TProviders>, options: MockContextOptions<TArgs, TProviders>, fn: () => R | Promise<R>): Promise<R>

Runs `fn` inside a mocked DI context using `AsyncLocalStorage.run()`. Unlike mockContext, this variant scopes the context properly across async boundaries — any `await` inside `fn` keeps seeing the mocked providers, and the context is automatically torn down when `fn` resolves (or throws). Prefer this for anything that isn't a trivial synchronous assertion.