Skip to content

Commit 01486c9

Browse files
committed
fix(jest-jasmine2): handle case when stack is not string
1 parent 3f5bc43 commit 01486c9

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

packages/jest-jasmine2/src/__tests__/__snapshots__/expectationResultFactory.test.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`expectationResultFactory returns the result if failed (with \`error.stack\` not as a string). 1`] = `
4+
"thrown: Object {
5+
\\"stack\\": Array [],
6+
}"
7+
`;
8+
39
exports[`expectationResultFactory returns the result if passed. 1`] = `
410
Object {
511
"error": undefined,

packages/jest-jasmine2/src/__tests__/expectationResultFactory.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,16 @@ describe('expectationResultFactory', () => {
7878
const result = expectationResultFactory(options);
7979
expect(result.message).toEqual('Expected `Pass`, received `Fail`.');
8080
});
81+
82+
it('returns the result if failed (with `error.stack` not as a string).', () => {
83+
const options = {
84+
actual: 'Fail',
85+
error: {stack: []},
86+
expected: 'Pass',
87+
matcherName: 'testMatcher',
88+
passed: false,
89+
};
90+
const result = expectationResultFactory(options);
91+
expect(result.message).toMatchSnapshot();
92+
});
8193
});

packages/jest-jasmine2/src/reporter.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,19 @@ export default class Jasmine2Reporter implements Reporter {
113113
return this._resultsPromise;
114114
}
115115

116-
private _addMissingMessageToStack(stack: string, message?: string) {
116+
private _addMissingMessageToStack(stack: unknown, message?: string) {
117117
// Some errors (e.g. Angular injection error) don't prepend error.message
118118
// to stack, instead the first line of the stack is just plain 'Error'
119119
const ERROR_REGEX = /^Error:?\s*\n/;
120120

121-
if (stack && message && !stack.includes(message)) {
122-
return message + stack.replace(ERROR_REGEX, '\n');
121+
if (typeof stack === 'string') {
122+
if (stack && message && !stack.includes(message)) {
123+
return message + stack.replace(ERROR_REGEX, '\n');
124+
}
125+
return stack;
126+
} else {
127+
return `${stack}`;
123128
}
124-
return stack;
125129
}
126130

127131
private _extractSpecResults(

0 commit comments

Comments
 (0)