diff --git a/__tests__/errorController.test.ts b/__tests__/errorController.test.ts index e723193..04e5b39 100644 --- a/__tests__/errorController.test.ts +++ b/__tests__/errorController.test.ts @@ -1,5 +1,7 @@ import { Request, Response, NextFunction } from 'express'; -import { notFound, errorHandler } from '../server/controllers/errorControllers'; +import { notFound } from '../server/controllers/errorControllers'; +import errorHandler from '../server/middleware/errorHandler'; +import { BadRequestError } from '../server/errors'; describe('Middleware Tests', () => { let mockRequest: Partial; @@ -10,7 +12,7 @@ describe('Middleware Tests', () => { mockRequest = {}; mockResponse = { status: jest.fn().mockReturnThis(), - json: jest.fn(), + send: jest.fn(), }; mockNext = jest.fn(); }); @@ -25,14 +27,15 @@ describe('Middleware Tests', () => { describe('errorHandler Middleware', () => { it('should handle the error correctly', () => { - const mockError = new Error('Some error'); + const mockError = new BadRequestError('Some error'); errorHandler(mockError, mockRequest as Request, mockResponse as Response, mockNext); expect(mockResponse.status).toHaveBeenCalledWith(400); - expect(mockResponse.json).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.anything(), - stack: expect.any(String), - }), + expect(mockResponse.send).toHaveBeenCalledWith( + expect.objectContaining([ + { + message: expect.any(String), + }, + ]), ); }); });