Skip to content

Commit

Permalink
Add tests for mongoose error conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed Dec 24, 2020
1 parent ea4f1c5 commit faf9944
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions tests/unit/middlewares/error.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const mongoose = require('mongoose');
const httpStatus = require('http-status');
const httpMocks = require('node-mocks-http');
const { errorConverter, errorHandler } = require('../../../src/middlewares/error');
Expand All @@ -11,7 +12,7 @@ describe('Error middlewares', () => {
const error = new ApiError(httpStatus.BAD_REQUEST, 'Any error');
const next = jest.fn();

errorConverter(error, httpMocks.createRequest(), httpMocks.createResponse, next);
errorConverter(error, httpMocks.createRequest(), httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith(error);
});
Expand All @@ -21,7 +22,7 @@ describe('Error middlewares', () => {
error.statusCode = httpStatus.BAD_REQUEST;
const next = jest.fn();

errorConverter(error, httpMocks.createRequest(), httpMocks.createResponse, next);
errorConverter(error, httpMocks.createRequest(), httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith(expect.any(ApiError));
expect(next).toHaveBeenCalledWith(
Expand All @@ -37,7 +38,7 @@ describe('Error middlewares', () => {
const error = new Error('Any error');
const next = jest.fn();

errorConverter(error, httpMocks.createRequest(), httpMocks.createResponse, next);
errorConverter(error, httpMocks.createRequest(), httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith(expect.any(ApiError));
expect(next).toHaveBeenCalledWith(
Expand All @@ -54,7 +55,7 @@ describe('Error middlewares', () => {
error.statusCode = httpStatus.BAD_REQUEST;
const next = jest.fn();

errorConverter(error, httpMocks.createRequest(), httpMocks.createResponse, next);
errorConverter(error, httpMocks.createRequest(), httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith(expect.any(ApiError));
expect(next).toHaveBeenCalledWith(
Expand All @@ -66,11 +67,27 @@ describe('Error middlewares', () => {
);
});

test('should convert a Mongoose error to ApiError with status 400 and preserve its message', () => {
const error = new mongoose.Error('Any mongoose error');
const next = jest.fn();

errorConverter(error, httpMocks.createRequest(), httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith(expect.any(ApiError));
expect(next).toHaveBeenCalledWith(
expect.objectContaining({
statusCode: httpStatus.BAD_REQUEST,
message: error.message,
isOperational: false,
})
);
});

test('should convert any other object to ApiError with status 500 and its message', () => {
const error = {};
const next = jest.fn();

errorConverter(error, httpMocks.createRequest(), httpMocks.createResponse, next);
errorConverter(error, httpMocks.createRequest(), httpMocks.createResponse(), next);

expect(next).toHaveBeenCalledWith(expect.any(ApiError));
expect(next).toHaveBeenCalledWith(
Expand Down

0 comments on commit faf9944

Please sign in to comment.