Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Commit

Permalink
Adding more tests for createError
Browse files Browse the repository at this point in the history
  • Loading branch information
RC-Lee committed Feb 18, 2022
1 parent 4b4fd22 commit 79356c8
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,12 +893,75 @@ describe('hash', () => {
describe('Create Error tests for Satellite', () => {
test('should be an instance of type Error', () => {
expect(createError(404, 'testing') instanceof Error).toBe(true);

const testError = createError(404, 'Test for Errors');
const nestedError = createError(testError);
expect(nestedError instanceof Error).toBe(true);
});

test("should have it's value, and message accessible through it's members", () => {
const testError = createError(404, 'Satellite Test for Errors');
expect(testError.status).toBe(404);
expect(testError.message).toBe('Satellite Test for Errors');

const nestedError = createError(503, testError);
// error status of an Error instance will not be overwritten
expect(nestedError.status).toBe(404);
expect(nestedError.message).toBe('Satellite Test for Errors');
});

test('should create an Error from an object', () => {
const testObj = { key1: 'testing object', key2: 'for createError' };
const testError = createError(404, testObj);
expect(testError instanceof Error).toBe(true);
expect(testError.key1).toBe('testing object');
expect(testError.key2).toBe('for createError');
});

test('should fail when directly creating Error from ElasticSearch error object', () => {
// { errors } is imported from ElasticSearch
const elasticError = new errors.ResponseError({
body: { error: 'testing ElasticSearch Error' },
statusCode: 404,
});

try {
createError(503, elasticError);
} catch (err) {
expect(err instanceof TypeError).toBe(true);
expect(err.message).toBe(
'Cannot set property statusCode of [object Object] which has only a getter'
);
}
});

test('should create Error when indirectly creating from ElasticSearch error object', () => {
const elasticError = new errors.ResponseError({
body: {
errors: { error1: 'one ES error', error2: 'another ES error' },
status: 404,
error: 'test ElasticSearch Error',
},
statusCode: 404,
headers: {},
meta: {},
});

let testError = createError(503, elasticError.name, elasticError.body);

// Error status will be overwritten
expect(testError.status).toBe(503);
expect(testError.name).toBe('ServiceUnavailableError');
expect(testError.message).toBe('ResponseError');
expect(testError.errors).toStrictEqual({ error1: 'one ES error', error2: 'another ES error' });
expect(testError.error).toBe('test ElasticSearch Error');

testError = createError(elasticError.statusCode, elasticError.body);
expect(testError.status).toBe(404);
expect(testError.name).toBe('NotFoundError');
expect(testError.message).toBe('Not Found');
expect(testError.errors).toStrictEqual({ error1: 'one ES error', error2: 'another ES error' });
expect(testError.error).toBe('test ElasticSearch Error');
});
});

Expand Down

0 comments on commit 79356c8

Please sign in to comment.