@gp_jcisneros/errors
Version:
Error handling utilities for GreenPay microservices
161 lines (133 loc) • 5.25 kB
JavaScript
const { CustomError } = require('../src/CustomError');
describe('CustomError', () => {
test('should create a basic error', () => {
const error = new CustomError('Test error');
expect(error.message).toBe('Test error');
expect(error.statusCode).toBe(500);
expect(error.name).toBe('CustomError');
expect(error.timestamp).toBeDefined();
});
test('should create error with required fields', () => {
const error = new CustomError('Test error', {
errorCode: 'GP_TEST_ERROR',
description: 'Test error description',
integration: 'test-service',
statusCode: 400
});
expect(error.errorCode).toBe('GP_TEST_ERROR');
expect(error.description).toBe('Test error description');
expect(error.integration).toBe('test-service');
expect(error.statusCode).toBe(400);
});
test('should have default status code', () => {
const error = new CustomError('Test error');
expect(error.statusCode).toBe(500);
});
test('should use message as default description', () => {
const error = new CustomError('Test error message');
expect(error.description).toBe('Test error message');
});
test('should set required fields using setters', () => {
const error = new CustomError('Test error');
error.setErrorCode('GP_CUSTOM_ERROR');
error.setDescription('Custom error description');
error.setIntegration('custom-service');
expect(error.errorCode).toBe('GP_CUSTOM_ERROR');
expect(error.description).toBe('Custom error description');
expect(error.integration).toBe('custom-service');
});
test('should set all required fields at once', () => {
const error = new CustomError('Test error');
error.setRequiredFields('GP_SET_ERROR', 'Set error description', 'set-service');
expect(error.errorCode).toBe('GP_SET_ERROR');
expect(error.description).toBe('Set error description');
expect(error.integration).toBe('set-service');
});
test('should get required fields', () => {
const error = new CustomError('Test error', {
errorCode: 'GP_GET_ERROR',
description: 'Get error description',
integration: 'get-service'
});
const requiredFields = error.getRequiredFields();
expect(requiredFields).toEqual({
errorCode: 'GP_GET_ERROR',
description: 'Get error description',
integration: 'get-service'
});
});
test('should check if has required fields', () => {
const errorWithFields = new CustomError('Test error', {
errorCode: 'GP_COMPLETE_ERROR',
description: 'Complete error description',
integration: 'complete-service'
});
const errorWithoutFields = new CustomError('Test error');
expect(errorWithFields.hasRequiredFields()).toBe(true);
expect(errorWithoutFields.hasRequiredFields()).toBe(false);
});
test('should convert to JSON', () => {
const error = new CustomError('Test error', {
errorCode: 'GP_JSON_ERROR',
description: 'JSON error description',
integration: 'json-service',
statusCode: 400
});
const json = error.toJSON();
expect(json.name).toBe('CustomError');
expect(json.message).toBe('Test error');
expect(json.statusCode).toBe(400);
expect(json.errorCode).toBe('GP_JSON_ERROR');
expect(json.description).toBe('JSON error description');
expect(json.integration).toBe('json-service');
expect(json.timestamp).toBeDefined();
expect(json.stack).toBeDefined();
});
test('should add additional fields', () => {
const error = new CustomError('Test error', {
errorCode: 'GP_ADDITIONAL_ERROR',
description: 'Additional error description',
integration: 'additional-service',
customField: 'custom value',
metadata: { key: 'value' }
});
expect(error.customField).toBe('custom value');
expect(error.metadata).toEqual({ key: 'value' });
});
test('should create error using static create method', () => {
const error = CustomError.create(
'Test error',
'GP_STATIC_ERROR',
'Static error description',
'static-service',
400
);
expect(error.message).toBe('Test error');
expect(error.errorCode).toBe('GP_STATIC_ERROR');
expect(error.description).toBe('Static error description');
expect(error.integration).toBe('static-service');
expect(error.statusCode).toBe(400);
});
test('should create error using static createMinimal method', () => {
const error = CustomError.createMinimal(
'Test error',
'GP_MINIMAL_ERROR',
'minimal-service'
);
expect(error.message).toBe('Test error');
expect(error.errorCode).toBe('GP_MINIMAL_ERROR');
expect(error.integration).toBe('minimal-service');
expect(error.description).toBe('Test error'); // Should default to message
expect(error.statusCode).toBe(500); // Should default to 500
});
test('should handle null and undefined values', () => {
const error = new CustomError('Test error', {
errorCode: null,
description: undefined,
integration: null
});
expect(error.errorCode).toBe(null);
expect(error.description).toBe('Test error'); // Should default to message
expect(error.integration).toBe(null);
});
});