From 4a042e7781ccbbf73e9bf924a4384385837918e8 Mon Sep 17 00:00:00 2001 From: Andy Terranova <13182778+supernova-at@users.noreply.github.com> Date: Mon, 11 Feb 2019 13:56:32 -0600 Subject: [PATCH] Adds unit tests for UpwardServerError.js (#868) --- packages/upward-js/lib/UpwardServerError.js | 5 +- .../lib/__tests__/UpwardServerError.test.js | 58 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 packages/upward-js/lib/__tests__/UpwardServerError.test.js diff --git a/packages/upward-js/lib/UpwardServerError.js b/packages/upward-js/lib/UpwardServerError.js index a7a4ffeac9..59a1488225 100644 --- a/packages/upward-js/lib/UpwardServerError.js +++ b/packages/upward-js/lib/UpwardServerError.js @@ -4,7 +4,10 @@ class UpwardServerError extends Error { } constructor(originalError, message) { super(originalError); - this.message += ' -- ' + message; + + if (message) { + this.message += ' -- ' + message; + } } } diff --git a/packages/upward-js/lib/__tests__/UpwardServerError.test.js b/packages/upward-js/lib/__tests__/UpwardServerError.test.js new file mode 100644 index 0000000000..e05efb4040 --- /dev/null +++ b/packages/upward-js/lib/__tests__/UpwardServerError.test.js @@ -0,0 +1,58 @@ +import UpwardServerError from '../UpwardServerError'; + +const MOCK_ERROR_MESSAGE = 'Unit Test'; +const MOCK_ERROR = new Error(MOCK_ERROR_MESSAGE); +const MOCK_ADDITIONAL_MESSAGE = 'Additional Unit Test Message'; + +describe('class', () => { + test('it is of type Error', () => { + const result = new UpwardServerError( + MOCK_ERROR, + MOCK_ADDITIONAL_MESSAGE + ); + + expect(result).toBeInstanceOf(Error); + }); + + test('it can accept an error object as its first argument', () => { + const result = new UpwardServerError(MOCK_ERROR); + + expect(result).toBeInstanceOf(Error); + }); + + test('it can accept a string as its first argument', () => { + const result = new UpwardServerError(MOCK_ERROR_MESSAGE); + + expect(result).toBeInstanceOf(Error); + }); +}); + +describe('name', () => { + test('it equals "UpwardServerError"', () => { + const result = new UpwardServerError( + MOCK_ERROR, + MOCK_ADDITIONAL_MESSAGE + ); + + expect(result.name).toBe('UpwardServerError'); + }); +}); + +describe('message', () => { + test('it appends a custom message to the original error', () => { + const result = new UpwardServerError( + MOCK_ERROR, + MOCK_ADDITIONAL_MESSAGE + ); + + expect(result.message).toEqual( + `Error: ${MOCK_ERROR_MESSAGE} -- ${MOCK_ADDITIONAL_MESSAGE}` + ); + }); + + test('it does not append a custom message if one is not present', () => { + const result = new UpwardServerError(MOCK_ERROR); + + expect(result.message).toEqual(`Error: ${MOCK_ERROR_MESSAGE}`); + }); +});