/
test-is-required-flag.js
72 lines (64 loc) 路 2.36 KB
/
test-is-required-flag.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
import test from 'ava';
import execa from 'execa';
const path = require('path');
const fixtureRequiredPath = path.join(__dirname, 'fixtures', 'fixture-required.js');
const fixtureRequiredFunctionPath = path.join(__dirname, 'fixtures', 'fixture-required-function.js');
test('spawn cli and test not specifying required flags', async t => {
try {
await execa(fixtureRequiredPath, []);
} catch (error) {
const {stderr, message} = error;
t.regex(message, /Command failed with exit code 2/);
t.regex(stderr, /Missing required flag/);
t.regex(stderr, /--test, -t/);
t.regex(stderr, /--number/);
t.notRegex(stderr, /--notRequired/);
}
});
test('spawn cli and test specifying all required flags', async t => {
const {stdout} = await execa(fixtureRequiredPath, [
'-t',
'test',
'--number',
'6'
]);
t.is(stdout, 'test,6');
});
test('spawn cli and test specifying required string flag with an empty string as value', async t => {
try {
await execa(fixtureRequiredPath, ['--test', '']);
} catch (error) {
const {stderr, message} = error;
t.regex(message, /Command failed with exit code 2/);
t.regex(stderr, /Missing required flag/);
t.notRegex(stderr, /--test, -t/);
}
});
test('spawn cli and test specifying required number flag without a number', async t => {
try {
await execa(fixtureRequiredPath, ['--number']);
} catch (error) {
const {stderr, message} = error;
t.regex(message, /Command failed with exit code 2/);
t.regex(stderr, /Missing required flag/);
t.regex(stderr, /--number/);
}
});
test('spawn cli and test setting isRequired as a function and not specifying any flags', async t => {
const {stdout} = await execa(fixtureRequiredFunctionPath, []);
t.is(stdout, 'false,undefined');
});
test('spawn cli and test setting isRequired as a function and specifying only the flag that activates the isRequired condition for the other flag', async t => {
try {
await execa(fixtureRequiredFunctionPath, ['--trigger']);
} catch (error) {
const {stderr, message} = error;
t.regex(message, /Command failed with exit code 2/);
t.regex(stderr, /Missing required flag/);
t.regex(stderr, /--withTrigger/);
}
});
test('spawn cli and test setting isRequired as a function and specifying both the flags', async t => {
const {stdout} = await execa(fixtureRequiredFunctionPath, ['--trigger', '--withTrigger', 'specified']);
t.is(stdout, 'true,specified');
});