import { Request, Response, NextFunction } from 'express'; import { asyncRoute } from '../../src/asyncRoute'; function fakeArgs() { const next = jest.fn() as unknown as NextFunction; return { req: {} as Request, res: {} as Response, next }; } describe('asyncRoute', () => { it('passes a rejected handler to next so Express can answer the request', async () => { const boom = new Error('relation "categories" does not exist'); const { req, res, next } = fakeArgs(); await asyncRoute(async () => { throw boom; })(req, res, next); expect(next).toHaveBeenCalledWith(boom); }); it('passes a synchronous throw to next as well', async () => { const boom = new Error('sync failure'); const { req, res, next } = fakeArgs(); await asyncRoute(() => { throw boom; })(req, res, next); expect(next).toHaveBeenCalledWith(boom); }); it('leaves a successful handler alone', async () => { const { req, res, next } = fakeArgs(); await asyncRoute(async () => 'fine')(req, res, next); expect(next).not.toHaveBeenCalled(); }); it('forwards the same arguments it was given', async () => { const handler = jest.fn(async () => undefined); const { req, res, next } = fakeArgs(); await asyncRoute(handler)(req, res, next); expect(handler).toHaveBeenCalledWith(req, res, next); }); });