diff --git a/test/middleware.ts b/test/middleware.ts index 80599527..7924a1a3 100644 --- a/test/middleware.ts +++ b/test/middleware.ts @@ -1,3 +1,4 @@ +import { UnknownError } from 'http-errors'; import { ExtendableContext } from 'koa'; import { Middleware } from 'koa-compose'; import { AppContext } from '../src/app'; @@ -5,6 +6,11 @@ import { AppContext } from '../src/app'; export type NextMiddleware = (context: CustomT) => Promise; +export const throwingNext = + (error: UnknownError = new Error()): NextMiddleware => (): never => { + throw error; + }; + export default async ( middleware: Middleware, context: CustomT, diff --git a/test/middleware/error-handler.test.ts b/test/middleware/error-handler.test.ts index 4752e28b..710831e8 100644 --- a/test/middleware/error-handler.test.ts +++ b/test/middleware/error-handler.test.ts @@ -1,5 +1,5 @@ import { blankNode, literal, quad } from '@rdfjs/data-model'; -import createHttpError, { UnknownError } from 'http-errors'; +import createHttpError from 'http-errors'; import { INTERNAL_SERVER_ERROR, SERVICE_UNAVAILABLE } from 'http-status-codes'; import 'jest-rdf'; import { Response } from 'koa'; @@ -7,20 +7,16 @@ 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> => ( runMiddleware(errorHandler(), createContext({ errorListener }), next) ); -const next = (error: UnknownError) => async (): Promise => { - 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 => { - const response = await makeRequest(next(new createHttpError.ServiceUnavailable())); + const response = await makeRequest(throwingNext(new createHttpError.ServiceUnavailable())); expect(response.status).toBe(SERVICE_UNAVAILABLE); }); @@ -29,14 +25,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 => { - const response = await makeRequest(next(new createHttpError.ServiceUnavailable())); + const response = await makeRequest(throwingNext(new createHttpError.ServiceUnavailable())); const id = blankNode(); const expected = [ @@ -50,7 +46,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 => { - const response = await makeRequest(next('some-error')); + const response = await makeRequest(throwingNext()); expect(response.status).toBe(INTERNAL_SERVER_ERROR); }); @@ -59,14 +55,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 => { - const response = await makeRequest(next('Some Error')); + const response = await makeRequest(throwingNext('Some Error')); const id = blankNode(); const expected = [