-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathcli.test.mjs
More file actions
79 lines (69 loc) · 2.04 KB
/
cli.test.mjs
File metadata and controls
79 lines (69 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import assert from 'node:assert/strict';
import { describe, it, mock } from 'node:test';
const logger = {
setLogLevel: mock.fn(),
};
describe('bin/cli', () => {
it('builds a program with commands/options and runs preAction hook', async () => {
const action = mock.fn(async () => {});
const commands = [
{
name: 'mycmd',
description: 'My command',
options: {
requiredText: {
flags: ['--required-text <value>'],
desc: 'Required option',
prompt: { type: 'text', required: true },
},
multi: {
flags: ['--multi <values...>'],
desc: 'Multi option',
prompt: {
type: 'multiselect',
options: [{ value: 'a' }, { value: 'b' }],
initialValue: ['a'],
},
},
},
action,
},
];
const { createProgram } = await import('../cli.mjs');
const program = createProgram(commands, { loggerInstance: logger })
.exitOverride()
.configureOutput({
writeOut: () => {},
writeErr: () => {},
});
// Global option should be present
const logLevelOpt = program.options.find(
o => o.attributeName() === 'logLevel'
);
assert.ok(logLevelOpt);
// Command and its options should be registered
const mycmd = program.commands.find(c => c.name() === 'mycmd');
assert.ok(mycmd);
const requiredOpt = mycmd.options.find(
o => o.attributeName() === 'requiredText'
);
assert.ok(requiredOpt);
assert.equal(requiredOpt.mandatory, true);
const multiOpt = mycmd.options.find(o => o.attributeName() === 'multi');
assert.ok(multiOpt);
assert.deepEqual(multiOpt.argChoices, ['a', 'b']);
await program.parseAsync([
'node',
'cli',
'--log-level',
'debug',
'mycmd',
'--required-text',
'hello',
'--multi',
'a',
]);
assert.equal(logger.setLogLevel.mock.callCount(), 1);
assert.equal(action.mock.callCount(), 1);
});
});