Skip to content

Commit

Permalink
Update test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimyr committed Nov 20, 2019
1 parent 169f50c commit 9fa7814
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ const express = require('express');
const supertest = require('supertest');
const assert = require('assert');

const last = (arr) => arr[arr.length - 1];

describe('express-async-errors', () => {
it('propagates routes errors to error handler', () => {
it('should propagate route handler errors to error handler', () => {
const app = express();

app.get('/test', async () => {
throw new Error('error');
});

app.use((err, req, res, next) => {
res.status(495);
res.end();
res.status(495).end();
});

return supertest(app)
.get('/test')
.expect(495);
});

it('propagates regular middleware errors too', () => {
it('should propagate middleware errors to error handler', () => {
const app = express();

app.use(async () => {
Expand All @@ -37,16 +38,15 @@ describe('express-async-errors', () => {
});

app.use((err, req, res, next) => {
res.status(495);
res.end();
res.status(495).end();
});

return supertest(app)
.get('/test')
.expect(495);
});

it('and propagates error middleware errors too', () => {
it('should propagate error middleware errors to error handler', () => {
const app = express();

app.get('/test', async () => {
Expand All @@ -58,16 +58,15 @@ describe('express-async-errors', () => {
});

app.use((err, req, res, next) => {
res.status(495);
res.end();
res.status(495).end();
});

return supertest(app)
.get('/test')
.expect(495);
});

it('and propagates param middleware errors too', () => {
it('should propagate param middleware errors to error handler', () => {
const app = express();

app.param('id', async () => {
Expand All @@ -79,8 +78,7 @@ describe('express-async-errors', () => {
});

app.use((err, req, res, next) => {
res.status(495);
res.end();
res.status(495).end();
});

return supertest(app)
Expand All @@ -93,8 +91,8 @@ describe('express-async-errors', () => {

function swaggerize(item) {
function describeRouterRoute(router, metaData) {
const lastRoute = router.stack[router.stack.length - 1];
const verb = Object.keys(lastRoute.route.methods)[0];
const lastRoute = last(router.stack);
const [verb] = Object.keys(lastRoute.route.methods);
metaData.path = lastRoute.route.path;
metaData.verb = verb;
lastRoute.route.swaggerData = metaData;
Expand All @@ -118,16 +116,17 @@ describe('express-async-errors', () => {

router
.get('/test', (req, res) => {
res.status(200).send('Ok');
res.status(200).end();
})
.describe({ hasDescription: true });

app.use('/', router);

const appRouteStack = app._router.stack;
const someMiddlewareFunctionStack = appRouteStack[appRouteStack.length - 1];
const someMiddlewareFunctionStack = last(appRouteStack);
const innerStack = someMiddlewareFunctionStack.handle.stack;
const routeData = innerStack[0].route.swaggerData;

assert.ok(routeData);
assert.equal(routeData.verb, 'get');
assert.equal(routeData.hasDescription, true);
Expand Down

0 comments on commit 9fa7814

Please sign in to comment.