Skip to content

Commit

Permalink
Write error handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed Nov 3, 2019
1 parent b0458a4 commit 7ce2997
Showing 1 changed file with 79 additions and 1 deletion.
80 changes: 79 additions & 1 deletion tests/unit/middlewares/error.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
const httpStatus = require('http-status');
const httpMocks = require('node-mocks-http');
const { errorConverter } = require('../../../src/middlewares/error');
const { errorConverter, errorHandler } = require('../../../src/middlewares/error');
const AppError = require('../../../src/utils/AppError');
const config = require('../../../src/config/config');
const logger = require('../../../src/config/logger');

describe('Error middlewares', () => {
describe('Error converter', () => {
test('should return the same AppError object it was called with', () => {
const error = new AppError(httpStatus.BAD_REQUEST, 'Any error');
const next = jest.fn();

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

expect(next).toHaveBeenCalledWith(error);
});

test('should convert an Error to AppError and preserve its status and message', () => {
const error = new Error('Any error');
error.statusCode = httpStatus.BAD_REQUEST;
const next = jest.fn();

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

expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -30,7 +36,9 @@ describe('Error middlewares', () => {
test('should convert an Error without status to AppError with status 500', () => {
const error = new Error('Any error');
const next = jest.fn();

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

expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -45,7 +53,9 @@ describe('Error middlewares', () => {
const error = new Error();
error.statusCode = httpStatus.BAD_REQUEST;
const next = jest.fn();

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

expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -59,7 +69,9 @@ describe('Error middlewares', () => {
test('should convert any other object to AppError with status 500 and its message', () => {
const error = {};
const next = jest.fn();

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

expect(next).toHaveBeenCalledWith(expect.any(AppError));
expect(next).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -70,4 +82,70 @@ describe('Error middlewares', () => {
);
});
});

describe('Error handler', () => {
beforeEach(() => {
jest.spyOn(logger, 'error').mockImplementation(() => {});
});

test('should send proper error response and put the error message in res.locals', () => {
const error = new AppError(httpStatus.BAD_REQUEST, 'Any error');
const res = httpMocks.createResponse();
const sendSpy = jest.spyOn(res, 'send');

errorHandler(error, httpMocks.createRequest(), res);

expect(sendSpy).toHaveBeenCalledWith(expect.objectContaining({ code: error.statusCode, message: error.message }));
expect(res.locals.errorMessage).toBe(error.message);
});

test('should put the error stack in the response if in development mode', () => {
config.env = 'development';
const error = new AppError(httpStatus.BAD_REQUEST, 'Any error');
const res = httpMocks.createResponse();
const sendSpy = jest.spyOn(res, 'send');

errorHandler(error, httpMocks.createRequest(), res);

expect(sendSpy).toHaveBeenCalledWith(
expect.objectContaining({ code: error.statusCode, message: error.message, stack: error.stack })
);
config.env = process.env.NODE_ENV;
});

test('should send internal server error status and message if in production mode and error is not operational', () => {
config.env = 'production';
const error = new AppError(httpStatus.BAD_REQUEST, 'Any error', false);
const res = httpMocks.createResponse();
const sendSpy = jest.spyOn(res, 'send');

errorHandler(error, httpMocks.createRequest(), res);

expect(sendSpy).toHaveBeenCalledWith(
expect.objectContaining({
code: httpStatus.INTERNAL_SERVER_ERROR,
message: httpStatus[httpStatus.INTERNAL_SERVER_ERROR],
})
);
expect(res.locals.errorMessage).toBe(error.message);
config.env = process.env.NODE_ENV;
});

test('should preserve original error status and message if in production mode and error is operational', () => {
config.env = 'production';
const error = new AppError(httpStatus.BAD_REQUEST, 'Any error');
const res = httpMocks.createResponse();
const sendSpy = jest.spyOn(res, 'send');

errorHandler(error, httpMocks.createRequest(), res);

expect(sendSpy).toHaveBeenCalledWith(
expect.objectContaining({
code: error.statusCode,
message: error.message,
})
);
config.env = process.env.NODE_ENV;
});
});
});

0 comments on commit 7ce2997

Please sign in to comment.