Skip to main content

Using the Test Harness

This is a simple example that demonstrates how to create a basic CLI using cli-forge

Code

Using the Test Harness
import { describe, it } from 'node:test';
import * as assert from 'node:assert';

// We can reuse the CLI from the basic-cli example
import cli from './basic-cli';
import { TestHarness } from 'cli-forge';

describe('Basic CLI', () => {
it('should parse the hello command', async () => {
// The TestHarness is used to simulate CLI invocations, without actually running the commands.
// If you want to test how the CLI is parsing arguments, you can use the TestHarness.
// If you actually want to test the CLI handlers, you should extract the handler logic
// to a separate function and test that function directly.
const harness = new TestHarness(cli);

// The parse method returns the parsed arguments and the command chain that was resolved.
// The command chain contains each command that was resolved during parsing. If its empty,
// then the root command was resolved. If it contains ['hello'], then the hello command was resolved.
const { args, commandChain } = await harness.parse([
'hello',
'--name',
'sir',
]);

assert.deepStrictEqual(commandChain, ['hello']);
assert.deepStrictEqual(args, { name: 'sir', unmatched: [] });
});
});

View on TypeScript Playground

Usage

node --test ./test-harness.js

These examples are ran as e2e tests on pull-requests and releases to verify they are accurate and up to date. If you see any issues, please open an issue on the github repo.