forked from readmeio/rdme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
114 lines (93 loc) · 4.26 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const nock = require('nock');
const cli = require('../src');
const { version } = require('../package.json');
const conf = require('../src/lib/configstore');
describe('cli', () => {
it('command not found', async () => {
await expect(cli('notARealCommand')).rejects.toThrow('Command not found');
});
describe('--version', () => {
it('should return version from package.json', async () => {
await expect(cli(['--version'])).resolves.toBe(version);
});
it('should return version if the `-V` alias is supplied', async () => {
await expect(cli(['-V'])).resolves.toBe(version);
});
it('should return version from package.json for help command', async () => {
await expect(cli(['help', '--version'])).resolves.toBe(version);
});
// This is necessary because we use --version for other commands like `docs`
it('should only return version if no command', async () => {
await expect(cli(['no-such-command', '--version'])).rejects.toThrow(
// This can be ignored as it's just going to be a command not found error
'Command not found.'
);
});
it('should not be returned if `--version` is being used on a subcommand', async () => {
nock.disableNetConnect();
await expect(cli(['docs:edit', 'getting-started', '--version', '1.0.0', '--key=abcdef'])).rejects.not.toThrow(
// We're testing that the docs:edit command does NOT return an error about `--version` not
// being here because if it throws that error, then that means that `--version` wasn't
// passed in as expected.
'No project version provided. Please use `--version`.'
);
});
});
describe('--help', () => {
beforeEach(() => {
// The `table-layout` dependency within `command-line-usage` has a quirk in the 1.x family where if it's being
// run within a Jest environment it won't always use the same amount of columns for tables, resulting in cases
// where Jest snapshots will randomly return back different results.
//
// This issue has been fixed in later versions of `table-layout` but `command-line-usage` is still targeting the
// 1.x series.
//
// See https://github.com/75lb/table-layout/issues/9 for more information.
process.stdout.columns = 100;
process.stderr.columns = 100;
});
afterEach(() => {
process.stdout.columns = undefined;
process.stderr.columns = undefined;
});
it('should print help', async () => {
await expect(cli(['--help'])).resolves.toContain('a utility for interacting with ReadMe');
});
it('should print help for the `-H` alias', async () => {
await expect(cli(['-H'])).resolves.toContain('a utility for interacting with ReadMe');
});
it('should print usage for a given command', async () => {
await expect(cli(['openapi', '--help'])).resolves.toMatchSnapshot();
});
it('should print usage for a given command if supplied as `help <command>`', async () => {
await expect(cli(['help', 'openapi'])).resolves.toMatchSnapshot();
});
it('should not surface args that are designated as hidden', async () => {
await expect(cli(['openapi', '--help'])).resolves.toMatchSnapshot();
});
it('should show related commands for a subcommands help menu', async () => {
await expect(cli(['versions', '--help'])).resolves.toMatchSnapshot();
});
});
describe('subcommands', () => {
// docs:edit will make a backend connection
beforeAll(() => nock.disableNetConnect());
it('should load subcommands from the folder', async () => {
await expect(cli(['docs:edit', 'getting-started', '--version=1.0.0', '--key=abcdef'])).rejects.not.toThrow(
'Command not found.'
);
});
});
it('should not error with undefined cmd', async () => {
await expect(cli([])).resolves.toContain('Upload OpenAPI/Swagger definitions');
});
it('should add stored apiKey to opts', async () => {
expect.assertions(1);
conf.set('apiKey', '123456');
await expect(cli(['docs'])).rejects.toThrow('No folder provided. Usage `rdme docs <folder> [options]`.');
conf.delete('apiKey');
});
it('should error with `rdme oas` arguments passed in', async () => {
await expect(cli(['oas', 'endpoint'])).rejects.toThrow(/.*/);
});
});