Skip to content

Commit

Permalink
coverage for yaml malformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinchernev committed Oct 20, 2020
1 parent 1f2d9d6 commit 105f461
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
14 changes: 4 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { YAMLException } = require('js-yaml');
const getSpecificationObject = require('./helpers/getSpecificationObject');

const createSpecification = require('./helpers/createSpecification');
Expand Down Expand Up @@ -32,17 +33,10 @@ module.exports = (options) => {

return specificationObject;
} catch (err) {
let msg = err.message;
if (err.mark && err.mark.buffer && err.mark.line) {
const { line } = err.mark;
const bufferParts = err.mark.buffer.split('\n');
bufferParts[
line
] = `${bufferParts[line]} -------------- Pay attention at this place`;
msg = bufferParts.join('\n');
if (err instanceof YAMLException) {
console.log('Error in yaml file:', err.mark.buffer);
}
console.warn(msg);
throw new Error(err);
throw err;
}
};

Expand Down
33 changes: 33 additions & 0 deletions test/lib.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const path = require('path');
const { YAMLException } = require('js-yaml');
const swaggerJsdoc = require('../lib');

describe('swagger-jsdoc library', () => {
describe('Error handling', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.resetModules();
});

it('should require options input', () => {
expect(() => {
swaggerJsdoc();
Expand Down Expand Up @@ -30,6 +36,33 @@ describe('swagger-jsdoc library', () => {
`Missing or invalid input: 'options.apis' is required and it should be an array.`
);
});

it('should provide verbose information for wrongly formatted yaml inputs', () => {
jest.doMock('../lib/helpers/getSpecificationObject', () => {
throw new YAMLException('bad indentation of a mapping entry', {
name: null,
buffer: '/invalid_yaml:\n - foo\n bar\n\u0000',
position: 30,
line: 2,
column: 2,
});
});

expect(() => {
swaggerJsdoc({
swaggerDefinition: {
info: {
title: 'Hello World',
version: '1.0.0',
description: 'A sample API',
},
host: 'http://undefined:undefined',
basePath: '/',
},
apis: ['test/files/v2/wrong-yaml-identation1.js'],
});
}).toThrow();
});
});

describe('Specification v2: Swagger', () => {
Expand Down

0 comments on commit 105f461

Please sign in to comment.