-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathcli.spec.js
130 lines (111 loc) · 4.79 KB
/
cli.spec.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const fs = require('fs');
const { promisify } = require('util');
const { exec } = require('child_process');
const sh = promisify(exec);
const dir = process.env.PWD;
const bin = `${dir}/bin/swagger-jsdoc.js`;
describe('CLI module', () => {
it('help menu is default fallback when no arguments', async () => {
const result = await sh(`${bin}`);
expect(result.stdout).toMatchSnapshot();
});
it('help menu works', async () => {
const result = await sh(`${bin} -h`);
expect(result.stdout).toMatchSnapshot();
});
it('should require a definition file', async () => {
const result = await sh(`${bin} wrongDefinition`);
expect(result.stdout).toMatchSnapshot();
});
it('should require an info object in the definition', async () => {
const result = await sh(`${bin} -d test/files/v2/empty_definition.cjs`);
expect(result.stdout).toMatchSnapshot();
});
it('should require title and version in the info object', async () => {
const result = await sh(`${bin} -d test/files/v2/wrong_definition.cjs`);
expect(result.stdout).toMatchSnapshot();
});
it('should require arguments with jsDoc data about an API', async () => {
const result = await sh(`${bin} -d examples/app/swaggerDefinition.cjs`);
expect(result.stdout).toMatchSnapshot();
});
it('should create swagger.json by default when the API input is good', async () => {
const result = await sh(
`${bin} -d examples/app/swaggerDefinition.cjs examples/app/routes.js`
);
expect(result.stdout).toBe('Swagger specification is ready.\n');
const specification = fs.statSync('swagger.json');
expect(specification.nlink).not.toBe(0);
});
it('should create swagger.json by default when the API input is from definition file', async () => {
const result = await sh(
`${bin} -d test/files/v2/api_definition.cjs examples/app/routes.js`
);
expect(result.stdout).toBe('Swagger specification is ready.\n');
const specification = fs.statSync('swagger.json');
expect(specification.nlink).not.toBe(0);
});
it('should accept custom configuration for output specification', async () => {
const result = await sh(
`${bin} -d examples/app/swaggerDefinition.cjs examples/app/routes.js -o customSpec.json`
);
expect(result.stdout).toBe('Swagger specification is ready.\n');
const specification = fs.statSync('customSpec.json');
expect(specification.nlink).not.toBe(0);
});
it('should create a YAML swagger spec when a custom output configuration with a .yaml extension is used', async () => {
const result = await sh(
`${bin} -d examples/app/swaggerDefinition.cjs -o customSpec.yaml examples/app/routes.js`
);
expect(result.stdout).toBe('Swagger specification is ready.\n');
const specification = fs.statSync('customSpec.yaml');
expect(specification.nlink).not.toBe(0);
});
it('should allow a JavaScript definition file', async () => {
const result = await sh(
`${bin} -d test/files/v2/api_definition.cjs examples/app/routes.js`
);
expect(result.stdout).toBe('Swagger specification is ready.\n');
const specification = fs.statSync('swagger.json');
expect(specification.nlink).not.toBe(0);
});
it('should allow a JSON definition file', async () => {
const result = await sh(
`${bin} -d test/files/v2/api_definition.json examples/app/routes.js`
);
expect(result.stdout).toBe('Swagger specification is ready.\n');
const specification = fs.statSync('swagger.json');
expect(specification.nlink).not.toBe(0);
});
it('should allow a YAML definition file', async () => {
const result = await sh(
`${bin} -d test/files/v2/api_definition.yaml examples/app/routes.js`
);
expect(result.stdout).toBe('Swagger specification is ready.\n');
const specification = fs.statSync('swagger.json');
expect(specification.nlink).not.toBe(0);
});
it('should reject definition file with invalid YAML syntax', async () => {
const result = await sh(`${bin} -d test/files/v2/wrong_syntax.yaml`);
expect(result.stdout).toMatchSnapshot();
});
it('should reject definition file with invalid JSON syntax', async () => {
const result = await sh(`${bin} -d test/files/v2/wrong_syntax.json`);
expect(result.stdout).toMatchSnapshot();
});
it('should report YAML documents with errors', async () => {
const result = await sh(
`${bin} -d examples/app/swaggerDefinition.cjs test/files/v2/wrong-yaml-identation.js`
);
expect(result.stdout).toContain(
'Not all input has been taken into account at your final specification.'
);
expect(result.stderr).toMatchSnapshot();
});
afterAll(() => {
fs.unlinkSync(`${dir}/swagger.json`);
fs.unlinkSync(`${dir}/customSpec.json`);
fs.unlinkSync(`${dir}/customSpec.yaml`);
fs.unlinkSync(`${dir}/customSpec.yml`);
});
});