Skip to content

Commit

Permalink
test(message-utils): add message-utils testing (#3212)
Browse files Browse the repository at this point in the history
add testing to `src/util/message-utils.ts#catchError`

fix cases where stencil could print a zero-length string
  • Loading branch information
rwaskiewicz authored Jan 27, 2022
1 parent d9f87ac commit d1d34cc
Show file tree
Hide file tree
Showing 2 changed files with 277 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils/message-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
275 changes: 275 additions & 0 deletions src/utils/test/message-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -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<d.Diagnostic>({
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<d.Diagnostic>({
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<d.Diagnostic>({
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<d.Diagnostic>({
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<d.Diagnostic>({
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<d.Diagnostic>({
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<d.Diagnostic>({
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<d.Diagnostic>({
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<d.Diagnostic>({
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<d.Diagnostic>({
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);
});
});
});
});
});

0 comments on commit d1d34cc

Please sign in to comment.