Skip to content
This repository has been archived by the owner on May 14, 2020. It is now read-only.

Extract test middleware that always throws an error #196

Merged
merged 3 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions test/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { UnknownError } from 'http-errors';
import { ExtendableContext } from 'koa';
import { Middleware } from 'koa-compose';
import { AppContext } from '../src/app';

export type NextMiddleware<CustomT extends ExtendableContext = AppContext> =
(context: CustomT) => Promise<void>;

export const throwingNext = <CustomT extends ExtendableContext = AppContext>
(error: UnknownError = new Error()): NextMiddleware<CustomT> => (): never => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed return type to never, and removed async (seems to make no difference).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given there's no await inside roughly I would expect async not to be required, but who knows what kind of type checking is going on

throw error;
};

export default async <CustomT extends ExtendableContext>(
middleware: Middleware<CustomT>,
context: CustomT,
Expand Down
20 changes: 8 additions & 12 deletions test/middleware/error-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import { blankNode, literal, quad } from '@rdfjs/data-model';
import createHttpError, { UnknownError } from 'http-errors';
import createHttpError from 'http-errors';
import 'jest-rdf';
import { Response } from 'koa';
import { WithDataset } from '../../src/middleware/dataset';
import errorHandler from '../../src/middleware/error-handler';
import { hydra, rdf } from '../../src/namespaces';
import createContext, { ErrorListener } from '../context';
import runMiddleware, { NextMiddleware } from '../middleware';
import runMiddleware, { NextMiddleware, throwingNext } from '../middleware';

const makeRequest = async (next?: NextMiddleware, errorListener?: ErrorListener): Promise<WithDataset<Response>> => (
runMiddleware(errorHandler(), createContext({ errorListener }), next)
);

const next = (error: UnknownError) => async (): Promise<void> => {
throw error;
};

describe('error-handler middleware', (): void => {
describe('for an http error', (): void => {
it('should capture the error and return an error response', async (): Promise<void> => {
const response = await makeRequest(next(new createHttpError.ServiceUnavailable()));
const response = await makeRequest(throwingNext(new createHttpError.ServiceUnavailable()));

expect(response.status).toBe(503);
});
Expand All @@ -28,14 +24,14 @@ describe('error-handler middleware', (): void => {
const error = new createHttpError.ServiceUnavailable();
const errorListener = jest.fn();

const response = await makeRequest(next(error), errorListener);
const response = await makeRequest(throwingNext(error), errorListener);

expect(errorListener).toHaveBeenCalledTimes(1);
expect(errorListener).toHaveBeenCalledWith(error, expect.objectContaining({ response }));
});

it('should return details about the error', async (): Promise<void> => {
const response = await makeRequest(next(new createHttpError.ServiceUnavailable()));
const response = await makeRequest(throwingNext(new createHttpError.ServiceUnavailable()));

const id = blankNode();
const expected = [
Expand All @@ -49,7 +45,7 @@ describe('error-handler middleware', (): void => {

describe('for a non-http error', (): void => {
it('should capture the error and return an error response', async (): Promise<void> => {
const response = await makeRequest(next('some-error'));
const response = await makeRequest(throwingNext());

expect(response.status).toBe(500);
});
Expand All @@ -58,14 +54,14 @@ describe('error-handler middleware', (): void => {
const error = 'some-error';
const errorListener = jest.fn();

const response = await makeRequest(next(error), errorListener);
const response = await makeRequest(throwingNext(error), errorListener);

expect(errorListener).toHaveBeenCalledTimes(1);
expect(errorListener).toHaveBeenCalledWith(error, expect.objectContaining({ response }));
});

it('should return details about the error', async (): Promise<void> => {
const response = await makeRequest(next('Some Error'));
const response = await makeRequest(throwingNext('Some Error'));

const id = blankNode();
const expected = [
Expand Down