Using the Test Harness
This is a simple example that demonstrates how to create a basic CLI using cli-forge
1 FILESTry in Playground
All Example Files
FILE EXPLORER
test-harness.ts
1import { describe, it } from 'node:test';
2import * as assert from 'node:assert';
3
4// We can reuse the CLI from the basic-cli example
5import cli from './basic-cli';
6import { TestHarness } from 'cli-forge';
7
8describe('Basic CLI', () => {
9 it('should parse the hello command', async () => {
10 // The TestHarness is used to simulate CLI invocations, without actually running the commands.
11 // If you want to test how the CLI is parsing arguments, you can use the TestHarness.
12 // If you actually want to test the CLI handlers, you should extract the handler logic
13 // to a separate function and test that function directly.
14 const harness = new TestHarness(cli);
15
16 // The parse method returns the parsed arguments and the command chain that was resolved.
17 // The command chain contains each command that was resolved during parsing. If its empty,
18 // then the root command was resolved. If it contains ['hello'], then the hello command was resolved.
19 const { args, commandChain } = await harness.parse([
20 'hello',
21 '--name',
22 'sir',
23 ]);
24
25 assert.deepStrictEqual(commandChain, ['hello']);
26 assert.deepStrictEqual(args, { name: 'sir', unmatched: [] });
27 });
28});
29