Skip to content

Commit

Permalink
increase coverage for initial error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinchernev committed Oct 19, 2020
1 parent 9058113 commit 1f2d9d6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ const finalizeSpecificationObject = require('./helpers/finalizeSpecificationObje
* @returns {object} Output specification
*/
module.exports = (options) => {
if (!options) {
throw new Error(`Missing or invalid input: 'options' is required`);
}

if (!options.swaggerDefinition && !options.definition) {
throw new Error(
'Provided swaggerDefinition or definition attributes are incorrect.'
`Missing or invalid input: 'options.swaggerDefinition' or 'options.definition' is required`
);
}

if (!options.apis || !Array.isArray(options.apis)) {
throw new Error(
`Missing or invalid input: 'options.apis' is required and it should be an array.`
);
} else if (!options.apis) {
throw new Error('Provided apis attribute are incorrect.');
}

try {
Expand Down
30 changes: 30 additions & 0 deletions test/lib.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@ const path = require('path');
const swaggerJsdoc = require('../lib');

describe('swagger-jsdoc library', () => {
describe('Error handling', () => {
it('should require options input', () => {
expect(() => {
swaggerJsdoc();
}).toThrow(`Missing or invalid input: 'options' is required`);
});

it('should require a definition input', () => {
expect(() => {
swaggerJsdoc({});
}).toThrow(
`Missing or invalid input: 'options.swaggerDefinition' or 'options.definition' is required`
);
});

it('should require an api files input', () => {
expect(() => {
swaggerJsdoc({ definition: {} });
}).toThrow(
`Missing or invalid input: 'options.apis' is required and it should be an array.`
);

expect(() => {
swaggerJsdoc({ definition: {}, apis: {} });
}).toThrow(
`Missing or invalid input: 'options.apis' is required and it should be an array.`
);
});
});

describe('Specification v2: Swagger', () => {
it('should support multiple paths', () => {
let testObject = {
Expand Down

0 comments on commit 1f2d9d6

Please sign in to comment.