Skip to content

Commit 1f2d9d6

Browse files
committed
increase coverage for initial error handling
1 parent 9058113 commit 1f2d9d6

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

lib/index.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ const finalizeSpecificationObject = require('./helpers/finalizeSpecificationObje
1111
* @returns {object} Output specification
1212
*/
1313
module.exports = (options) => {
14+
if (!options) {
15+
throw new Error(`Missing or invalid input: 'options' is required`);
16+
}
17+
1418
if (!options.swaggerDefinition && !options.definition) {
1519
throw new Error(
16-
'Provided swaggerDefinition or definition attributes are incorrect.'
20+
`Missing or invalid input: 'options.swaggerDefinition' or 'options.definition' is required`
21+
);
22+
}
23+
24+
if (!options.apis || !Array.isArray(options.apis)) {
25+
throw new Error(
26+
`Missing or invalid input: 'options.apis' is required and it should be an array.`
1727
);
18-
} else if (!options.apis) {
19-
throw new Error('Provided apis attribute are incorrect.');
2028
}
2129

2230
try {

test/lib.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,36 @@ const path = require('path');
22
const swaggerJsdoc = require('../lib');
33

44
describe('swagger-jsdoc library', () => {
5+
describe('Error handling', () => {
6+
it('should require options input', () => {
7+
expect(() => {
8+
swaggerJsdoc();
9+
}).toThrow(`Missing or invalid input: 'options' is required`);
10+
});
11+
12+
it('should require a definition input', () => {
13+
expect(() => {
14+
swaggerJsdoc({});
15+
}).toThrow(
16+
`Missing or invalid input: 'options.swaggerDefinition' or 'options.definition' is required`
17+
);
18+
});
19+
20+
it('should require an api files input', () => {
21+
expect(() => {
22+
swaggerJsdoc({ definition: {} });
23+
}).toThrow(
24+
`Missing or invalid input: 'options.apis' is required and it should be an array.`
25+
);
26+
27+
expect(() => {
28+
swaggerJsdoc({ definition: {}, apis: {} });
29+
}).toThrow(
30+
`Missing or invalid input: 'options.apis' is required and it should be an array.`
31+
);
32+
});
33+
});
34+
535
describe('Specification v2: Swagger', () => {
636
it('should support multiple paths', () => {
737
let testObject = {

0 commit comments

Comments
 (0)