From 1f2d9d6873e7b6c024d32a8346025300534f6208 Mon Sep 17 00:00:00 2001 From: Kalin Chernev Date: Mon, 19 Oct 2020 09:55:33 +0200 Subject: [PATCH] increase coverage for initial error handling --- lib/index.js | 14 +++++++++++--- test/lib.spec.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index c6c75feb..13d0c826 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 { diff --git a/test/lib.spec.js b/test/lib.spec.js index 562be48d..bd9c746f 100644 --- a/test/lib.spec.js +++ b/test/lib.spec.js @@ -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 = {