Skip to content

Commit

Permalink
Linter (again) (kogosoftwarellc#276)
Browse files Browse the repository at this point in the history
* do not use shell glob

As it does not recurse

* exclude certain directories from linting

* express-openapi: Linting and formatting

* ignore fetch-openapi test outputs

* fetch-openapi: linting and formating

* fs-routes: linting and formating

* koa-openapi: linting and formating

* openapi-default-setter: linting and formating

* openapi-framework: linting and formating

* openapi-jsonschema-parameters: linting and formating

* openapi-request-coercer: linting and formating

* openapi-request-validator: linting and formating

* openapi-response-validator: linting and formating

* openapi-schema-validator: linting and formating

* openapi-security-handler: linting and formating
  • Loading branch information
bill-kitsune authored and jsdevel committed Dec 20, 2018
1 parent ae72e44 commit 88f9aa0
Show file tree
Hide file tree
Showing 400 changed files with 3,556 additions and 3,111 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"combine-coverage": "istanbul-combine packages/*/coverage/coverage*.json",
"coveralls": "cat ./coverage/lcov.info | coveralls -v",
"lint": "tslint ./packages/**/*.[j,t]s",
"lint": "tslint './packages/**/*.[j,t]s'",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand Down
133 changes: 108 additions & 25 deletions packages/express-openapi/test/initialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { initialize } from '../';
const expect = require('chai').expect;
const express = require('express');
const path = require('path');
const routesDir = path.resolve(__dirname, './sample-projects/basic-usage/api-routes');
const routesDir = path.resolve(
__dirname,
'./sample-projects/basic-usage/api-routes'
);
const validDocument = {
swagger: '2.0',
info: {
Expand All @@ -16,18 +19,85 @@ describe(require('../package.json').name, () => {
describe('.initialize()', () => {
describe('args validation', () => {
[
['args is not an object', null, /express-openapi: args must be an object/],
['args.app must be an express app', {}, /express-openapi: args.app must be an express app/],
['args.apiDoc required', {app: {}}, /express-openapi: args.apiDoc is required/],
['args.apiDoc not valid', {app: {}, apiDoc: {}, paths: []}, /express-openapi: args.apiDoc was invalid. See the output./],
['args.paths required', {app: {}, apiDoc: validDocument}, /express-openapi: args.paths is required/],
['args.paths non directory', {app: {}, apiDoc: validDocument, paths: 'asdasdfasdf'}, /express-openapi: args.paths contained a value that was not a path to a directory/],
['args.paths non directory', {app: {}, apiDoc: validDocument, paths: routesDir, docsPath: true}, /express-openapi: args.docsPath must be a string when given/],
['args.paths with invalid route', {app: {}, apiDoc: validDocument, paths: [{foo: '/foo', bar: {}}]}, /express-openapi: args.paths must consist of strings or valid route specifications/],
['args.paths with duplicates', {app: {}, apiDoc: validDocument, paths: [{path: '/foo', module: {}}, {path: '/foo', module:{}}]}, /express-openapi: args.paths produced duplicate urls/],
['args.errorTransformer', {app: {}, apiDoc: validDocument, paths: routesDir, errorTransformer: 'asdf'}, /express-openapi: args.errorTransformer must be a function when given/],
['args.externalSchemas', {app: {}, apiDoc: validDocument, paths: routesDir, externalSchemas: 'asdf'}, /express-openapi: args.externalSchemas must be a object when given/],
['args.securityHandlers', {app: {}, apiDoc: validDocument, paths: routesDir, securityHandlers: 'asdf'}, /express-openapi: args.securityHandlers must be a object when given/],
[
'args is not an object',
null,
/express-openapi: args must be an object/
],
[
'args.app must be an express app',
{},
/express-openapi: args.app must be an express app/
],
[
'args.apiDoc required',
{ app: {} },
/express-openapi: args.apiDoc is required/
],
[
'args.apiDoc not valid',
{ app: {}, apiDoc: {}, paths: [] },
/express-openapi: args.apiDoc was invalid. See the output./
],
[
'args.paths required',
{ app: {}, apiDoc: validDocument },
/express-openapi: args.paths is required/
],
[
'args.paths non directory',
{ app: {}, apiDoc: validDocument, paths: 'asdasdfasdf' },
/express-openapi: args.paths contained a value that was not a path to a directory/
],
[
'args.paths non directory',
{ app: {}, apiDoc: validDocument, paths: routesDir, docsPath: true },
/express-openapi: args.docsPath must be a string when given/
],
[
'args.paths with invalid route',
{ app: {}, apiDoc: validDocument, paths: [{ foo: '/foo', bar: {} }] },
/express-openapi: args.paths must consist of strings or valid route specifications/
],
[
'args.paths with duplicates',
{
app: {},
apiDoc: validDocument,
paths: [{ path: '/foo', module: {} }, { path: '/foo', module: {} }]
},
/express-openapi: args.paths produced duplicate urls/
],
[
'args.errorTransformer',
{
app: {},
apiDoc: validDocument,
paths: routesDir,
errorTransformer: 'asdf'
},
/express-openapi: args.errorTransformer must be a function when given/
],
[
'args.externalSchemas',
{
app: {},
apiDoc: validDocument,
paths: routesDir,
externalSchemas: 'asdf'
},
/express-openapi: args.externalSchemas must be a object when given/
],
[
'args.securityHandlers',
{
app: {},
apiDoc: validDocument,
paths: routesDir,
securityHandlers: 'asdf'
},
/express-openapi: args.securityHandlers must be a object when given/
]
].forEach((test: [string, object, RegExp]) => {
const description: string = test[0];
const args = test[1];
Expand All @@ -44,37 +114,48 @@ describe(require('../package.json').name, () => {
});
});

it('should throw an error when a route method apiDoc is invalid', function() {
it('should throw an error when a route method apiDoc is invalid', () => {
expect(() => {
const app = express();

initialize({
apiDoc: require('./sample-projects/with-invalid-method-doc/api-doc.js'),
app: app,
app,
docsPath: '/api-docs',
// See https://github.com/kogosoftwarellc/express-openapi-validation#argserrortransformer
// errorTransformer: null,
// we could just pass in "api-routes" if process.cwd() was set to this directory.
paths: path.resolve(__dirname, 'sample-projects', 'with-invalid-method-doc', 'api-routes')
paths: path.resolve(
__dirname,
'sample-projects',
'with-invalid-method-doc',
'api-routes'
)
});
}).to.throw(/express-openapi: args.apiDoc was invalid after populating paths. See the output./);
}).to.throw(
/express-openapi: args.apiDoc was invalid after populating paths. See the output./
);
});

it('should not throw an error when args.validateApiDoc is false and a route method apiDoc is invalid', () => {
const app = express();

initialize({
apiDoc: require('./sample-projects/with-invalid-method-doc/api-doc.js'),
app: app,
app,
docsPath: '/api-docs',
validateApiDoc: false,
paths: path.resolve(__dirname, 'sample-projects', 'with-invalid-method-doc', 'api-routes')
paths: path.resolve(
__dirname,
'sample-projects',
'with-invalid-method-doc',
'api-routes'
)
});
});

it('should return the built apiDoc', () => {
const expectedApiDoc = require(
'./../../../test/fixtures/basic-usage-api-doc-after-initialization.json');
const expectedApiDoc = require('./../../../test/fixtures/basic-usage-api-doc-after-initialization.json');
const initializedApp = initialize({
apiDoc: require('./sample-projects/basic-usage/api-doc.js'),
app: express(),
Expand All @@ -87,15 +168,17 @@ describe(require('../package.json').name, () => {
it('should require referenced parameter to exist', () => {
expect(() => {
require('./sample-projects/with-referenced-parameter-missing/app.js');
}).to.throw(/Invalid parameter \$ref or definition not found in apiDoc\.parameters: #\/parameters\/Boo/);

}).to.throw(
/Invalid parameter \$ref or definition not found in apiDoc\.parameters: #\/parameters\/Boo/
);
});

it('should require referenced response to exist', () => {
expect(() => {
require('./sample-projects/with-referenced-response-missing/app.js');
}).to.throw(/Invalid response \$ref or definition not found in apiDoc.responses: #\/responses\/SuccessResponse/);

}).to.throw(
/Invalid response \$ref or definition not found in apiDoc.responses: #\/responses\/SuccessResponse/
);
});

it('should not throw when security handlers are defined and no method doc exists on a handler', () => {
Expand Down
Loading

0 comments on commit 88f9aa0

Please sign in to comment.