UNPKG

ncc-prototype-email-alerts-sub

Version:

Northumberland Council Front End Email Alerts Subscription Prototype

66 lines (54 loc) 2.37 kB
import { errorHandlerMiddleware } from '../../middleware/globalErrorHandler'; import supertest from 'supertest'; import express from 'express'; import { CustomError } from '../../types/custom'; describe('globalErrorHandler', () => { let app: express.Application; beforeEach(() => { app = express(); }); afterEach(() => { jest.clearAllMocks(); }); // eslint-disable-next-line @typescript-eslint/no-explicit-any it('should handle 500 error and redirect to /error500', async () => { app.use((req, res, next) => { const error: CustomError = new Error('test error for testing middleware'); (error).status = 500; errorHandlerMiddleware(error, req, res, next); }); const response = await supertest(app).get('/test-url'); expect(response.header['location']).toBe('/error500'); }); // @typescript-eslint/no-explicit-any it('should handle 404 error and redirect to /error404', async () => { app.use((req, res, next) => { const error: CustomError = new Error('Not Found'); (error).status = 404; errorHandlerMiddleware(error, req, res, next); }); const response = await supertest(app).get('/test-url'); expect(response.status).toBe(302); expect(response.header['location']).toBe('/error404'); }); it('should handle error with default status 500 and redirect to /error500', async () => { app.use((req, res, next) => { const error: CustomError = new Error('Test error'); errorHandlerMiddleware(error, req, res, next); }); const response = await supertest(app).get('/test-url'); expect(response.status).toBe(302); expect(response.header['location']).toBe('/error500'); }); // @typescript-eslint/no-explicit-any it('should handle error with status other than 404 and redirect to /error500', async () => { app.use((req, res, next) => { const error: CustomError = new Error('Forbidden'); (error).status = 403; errorHandlerMiddleware(error, req, res, next); }); const response = await supertest(app).get('/test-url'); expect(response.status).toBe(302); expect(response.header['location']).toBe('/error500'); }); });