-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(message-utils): add message-utils testing (#3212)
add testing to `src/util/message-utils.ts#catchError` fix cases where stencil could print a zero-length string
- Loading branch information
1 parent
d9f87ac
commit d1d34cc
Showing
2 changed files
with
277 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |