Skip to content

Commit

Permalink
update errorHandler test
Browse files Browse the repository at this point in the history
  • Loading branch information
seantokuzo committed Jun 10, 2024
1 parent 6bcc572 commit 1aec4d8
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions __tests__/errorController.test.ts
Original file line number Diff line number Diff line change
@@ -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<Request>;
Expand All @@ -10,7 +12,7 @@ describe('Middleware Tests', () => {
mockRequest = {};
mockResponse = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
send: jest.fn(),
};
mockNext = jest.fn();
});
Expand All @@ -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),
},
]),
);
});
});
Expand Down

0 comments on commit 1aec4d8

Please sign in to comment.