/
command.default.test.js
92 lines (78 loc) · 3.14 KB
/
command.default.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const childProcess = require('child_process');
const commander = require('../');
const path = require('path');
const util = require('util');
const execFileAsync = util.promisify(childProcess.execFile);
describe('default executable command', () => {
// Calling node explicitly so pm works without file suffix cross-platform.
const pm = path.join(__dirname, './fixtures/pm');
test('when default subcommand and no command then call default', async() => {
const { stdout } = await execFileAsync('node', [pm]);
expect(stdout).toBe('default\n');
});
test('when default subcommand and unrecognised argument then call default with argument', async() => {
const { stdout } = await execFileAsync('node', [pm, 'an-argument']);
expect(stdout).toBe("default\n[ 'an-argument' ]\n");
});
test('when default subcommand and unrecognised option then call default with option', async() => {
const { stdout } = await execFileAsync('node', [pm, '--an-option']);
expect(stdout).toBe("default\n[ '--an-option' ]\n");
});
});
describe('default action command', () => {
function makeProgram() {
const program = new commander.Command();
const actionMock = jest.fn();
program
.command('other');
program
.command('default', { isDefault: true })
.allowUnknownOption()
.action(actionMock);
return { program, actionMock };
}
test('when default subcommand and no command then call default', () => {
const { program, actionMock } = makeProgram();
program.parse('node test.js'.split(' '));
expect(actionMock).toHaveBeenCalled();
});
test('when default subcommand and unrecognised argument then call default', () => {
const { program, actionMock } = makeProgram();
program.parse('node test.js an-argument'.split(' '));
expect(actionMock).toHaveBeenCalled();
});
test('when default subcommand and unrecognised option then call default', () => {
const { program, actionMock } = makeProgram();
program.parse('node test.js --an-option'.split(' '));
expect(actionMock).toHaveBeenCalled();
});
});
describe('default added command', () => {
function makeProgram() {
const actionMock = jest.fn();
const defaultCmd = new commander.Command('default')
.allowUnknownOption()
.action(actionMock);
const program = new commander.Command();
program
.command('other');
program
.addCommand(defaultCmd, { isDefault: true });
return { program, actionMock };
}
test('when default subcommand and no command then call default', () => {
const { program, actionMock } = makeProgram();
program.parse('node test.js'.split(' '));
expect(actionMock).toHaveBeenCalled();
});
test('when default subcommand and unrecognised argument then call default', () => {
const { program, actionMock } = makeProgram();
program.parse('node test.js an-argument'.split(' '));
expect(actionMock).toHaveBeenCalled();
});
test('when default subcommand and unrecognised option then call default', () => {
const { program, actionMock } = makeProgram();
program.parse('node test.js --an-option'.split(' '));
expect(actionMock).toHaveBeenCalled();
});
});