From a05edecb5a77acbf34a31d8dbed20ab8dc8e1198 Mon Sep 17 00:00:00 2001 From: Ryan Waskiewicz Date: Fri, 21 Jan 2022 08:21:11 -0500 Subject: [PATCH] test(message-utils): add message-utils testing add testing to src/util/message-utils.ts --- src/utils/message-utils.ts | 4 +- src/utils/test/message-utils.spec.ts | 275 +++++++++++++++++++++++++++ 2 files changed, 277 insertions(+), 2 deletions(-) create mode 100644 src/utils/test/message-utils.spec.ts diff --git a/src/utils/message-utils.ts b/src/utils/message-utils.ts index 8c03c0d6a74..70ca730578f 100644 --- a/src/utils/message-utils.ts +++ b/src/utils/message-utils.ts @@ -137,13 +137,13 @@ export const catchError = (diagnostics: d.Diagnostic[], err: Error | null | unde }; if (isString(msg)) { - diagnostic.messageText = msg; + diagnostic.messageText = msg.length ? msg : 'UNKNOWN ERROR'; } else if (err != null) { if (err.stack != null) { diagnostic.messageText = err.stack.toString(); } else { if (err.message != null) { - diagnostic.messageText = err.message.toString(); + diagnostic.messageText = err.message.length ? err.message : 'UNKNOWN ERROR'; } else { diagnostic.messageText = err.toString(); } diff --git a/src/utils/test/message-utils.spec.ts b/src/utils/test/message-utils.spec.ts new file mode 100644 index 00000000000..988ea285dc9 --- /dev/null +++ b/src/utils/test/message-utils.spec.ts @@ -0,0 +1,275 @@ +import type * as d from '../../declarations'; +import { catchError } from '../message-utils'; + +describe('message-utils', () => { + describe('catchError()', () => { + describe('called with no error, no message', () => { + it('returns a template diagnostic', () => { + const diagnostic = catchError([], null); + + expect(diagnostic).toEqual({ + level: 'error', + type: 'build', + header: 'Build Error', + messageText: 'build error', + relFilePath: null, + absFilePath: null, + lines: [], + }); + }); + + it('pushes a template diagnostic onto a collection of diagnostics', () => { + const diagnostics: d.Diagnostic[] = []; + + const diagnostic = catchError(diagnostics, null); + + expect(diagnostics).toHaveLength(1); + expect(diagnostics[0]).toBe(diagnostic); + }); + }); + + describe('called with an Error', () => { + describe('with a valid stacktrace', () => { + const stackTrace = 'test stack'; + let err: Error; + + beforeEach(() => { + err = new Error(); + err.stack = stackTrace; + }); + + it('returns a diagnostic', () => { + const diagnostic = catchError([], err); + + expect(diagnostic).toEqual({ + level: 'error', + type: 'build', + header: 'Build Error', + messageText: stackTrace, + relFilePath: null, + absFilePath: null, + lines: [], + }); + }); + + it('pushes a template diagnostic onto a collection of diagnostics', () => { + const diagnostics: d.Diagnostic[] = []; + + const diagnostic = catchError(diagnostics, err); + + expect(diagnostics).toHaveLength(1); + expect(diagnostics[0]).toBe(diagnostic); + }); + + describe('"task canceled"', () => { + const taskCanceledMessage = 'task canceled'; + + beforeEach(() => { + err.stack = taskCanceledMessage; + }); + + it('returns a diagnostic', () => { + const diagnostic = catchError([], err); + + expect(diagnostic).toEqual({ + level: 'error', + type: 'build', + header: 'Build Error', + messageText: taskCanceledMessage, + relFilePath: null, + absFilePath: null, + lines: [], + }); + }); + + it("doesn't push a template diagnostic", () => { + const diagnostics: d.Diagnostic[] = []; + + catchError(diagnostics, err); + + expect(diagnostics).toHaveLength(0); + }); + }); + }); + + describe('with a valid message', () => { + const message = 'test message'; + let err: Error; + + beforeEach(() => { + err = new Error(); + err.stack = undefined; + err.message = message; + }); + + it('returns a diagnostic', () => { + const diagnostic = catchError([], err); + + expect(diagnostic).toEqual({ + level: 'error', + type: 'build', + header: 'Build Error', + messageText: message, + relFilePath: null, + absFilePath: null, + lines: [], + }); + }); + + it('pushes a template diagnostic onto a collection of diagnostics', () => { + const diagnostics: d.Diagnostic[] = []; + + const diagnostic = catchError(diagnostics, err); + + expect(diagnostics).toHaveLength(1); + expect(diagnostics[0]).toBe(diagnostic); + }); + + it('prints "UNKNOWN ERROR" for an empty message', () => { + err.message = ''; + const diagnostic = catchError([], err); + + expect(diagnostic).toEqual({ + level: 'error', + type: 'build', + header: 'Build Error', + messageText: 'UNKNOWN ERROR', + relFilePath: null, + absFilePath: null, + lines: [], + }); + }); + + describe('"task canceled"', () => { + const taskCanceledMessage = 'task canceled'; + + beforeEach(() => { + err.message = taskCanceledMessage; + }); + + it('returns a diagnostic', () => { + const diagnostic = catchError([], err); + + expect(diagnostic).toEqual({ + level: 'error', + type: 'build', + header: 'Build Error', + messageText: taskCanceledMessage, + relFilePath: null, + absFilePath: null, + lines: [], + }); + }); + + it("doesn't push a template diagnostic", () => { + const diagnostics: d.Diagnostic[] = []; + + catchError(diagnostics, err); + + expect(diagnostics).toHaveLength(0); + }); + }); + }); + + describe('with an invalid message', () => { + let err: Error; + + beforeEach(() => { + err = new Error(); + err.message = undefined; + err.stack = undefined; + }); + + it('returns a diagnostic', () => { + const diagnostic = catchError([], err); + + expect(diagnostic).toEqual({ + level: 'error', + type: 'build', + header: 'Build Error', + messageText: 'Error', + relFilePath: null, + absFilePath: null, + lines: [], + }); + }); + + it('pushes a template diagnostic onto a collection of diagnostics', () => { + const diagnostics: d.Diagnostic[] = []; + + const diagnostic = catchError(diagnostics, err); + + expect(diagnostics).toHaveLength(1); + expect(diagnostics[0]).toBe(diagnostic); + }); + }); + }); + + describe('called with a message, but no error', () => { + const message = 'this is a test message'; + + it('returns a diagnostic with the message', () => { + const diagnostic = catchError([], null, message); + + expect(diagnostic).toEqual({ + level: 'error', + type: 'build', + header: 'Build Error', + messageText: message, + relFilePath: null, + absFilePath: null, + lines: [], + }); + }); + + it('pushes the diagnostic onto a collection of diagnostics', () => { + const diagnostics: d.Diagnostic[] = []; + + const diagnostic = catchError(diagnostics, null, message); + + expect(diagnostics).toHaveLength(1); + expect(diagnostics[0]).toBe(diagnostic); + }); + + it('prints "UNKNOWN ERROR" when the message text is empty', () => { + const diagnostic = catchError([], null, ''); + + expect(diagnostic).toEqual({ + level: 'error', + type: 'build', + header: 'Build Error', + messageText: 'UNKNOWN ERROR', + relFilePath: null, + absFilePath: null, + lines: [], + }); + }); + + describe('"task canceled"', () => { + const taskCanceledMessage = 'task canceled'; + + it('returns a diagnostic', () => { + const diagnostic = catchError([], null, taskCanceledMessage); + + expect(diagnostic).toEqual({ + level: 'error', + type: 'build', + header: 'Build Error', + messageText: taskCanceledMessage, + relFilePath: null, + absFilePath: null, + lines: [], + }); + }); + + it("doesn't push a template diagnostic", () => { + const diagnostics: d.Diagnostic[] = []; + + catchError([], null, taskCanceledMessage); + + expect(diagnostics).toHaveLength(0); + }); + }); + }); + }); +});