-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcommand.addCommand.test.js
69 lines (60 loc) · 2.13 KB
/
command.addCommand.test.js
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
const commander = require('../');
// simple sanity check subcommand works
test('when addCommand and specify subcommand then called', () => {
const program = new commander.Command();
const leafAction = jest.fn();
const sub = new commander.Command();
sub
.name('sub')
.action(leafAction);
program
.addCommand(sub);
program.parse('node test.js sub'.split(' '));
expect(leafAction).toHaveBeenCalled();
});
test('when commands added using .addCommand and .command then internals similar', () => {
const program1 = new commander.Command();
program1.command('sub');
const program2 = new commander.Command();
program2.addCommand(new commander.Command('sub'));
// This is a bit of a cheat to check using .addCommand() produces similar result to .command(),
// since .command() is well tested and understood.
const cmd1 = program1.commands[0];
const cmd2 = program2.commands[0];
expect(cmd1.parent).toBe(program1);
expect(cmd2.parent).toBe(program2);
for (const key of Object.keys(cmd1)) {
switch (typeof cmd1[key]) {
case 'string':
case 'boolean':
case 'number':
case 'undefined':
// Compare vaues in a way that will be readable in test failure message.
expect(`${key}:${cmd1[key]}`).toEqual(`${key}:${cmd2[key]}`);
break;
}
}
});
test('when command without name passed to .addCommand then throw', () => {
const program = new commander.Command();
const cmd = new commander.Command();
expect(() => {
program.addCommand(cmd);
}).toThrow();
});
test('when executable command without custom executableFile passed to .addCommand then throw', () => {
const program = new commander.Command();
const cmd = new commander.Command('sub');
cmd.command('exec', 'exec description');
expect(() => {
program.addCommand(cmd);
}).toThrow();
});
test('when executable command with custom executableFile passed to .addCommand then ok', () => {
const program = new commander.Command();
const cmd = new commander.Command('sub');
cmd.command('exec', 'exec description', { executableFile: 'custom' });
expect(() => {
program.addCommand(cmd);
}).not.toThrow();
});