Skip to content

Commit

Permalink
fix(jest-jasmine2): handle case when stack is not string
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Oct 25, 2020
1 parent 3f5bc43 commit 01486c9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`expectationResultFactory returns the result if failed (with \`error.stack\` not as a string). 1`] = `
"thrown: Object {
\\"stack\\": Array [],
}"
`;

exports[`expectationResultFactory returns the result if passed. 1`] = `
Object {
"error": undefined,
Expand Down
Expand Up @@ -78,4 +78,16 @@ describe('expectationResultFactory', () => {
const result = expectationResultFactory(options);
expect(result.message).toEqual('Expected `Pass`, received `Fail`.');
});

it('returns the result if failed (with `error.stack` not as a string).', () => {
const options = {
actual: 'Fail',
error: {stack: []},
expected: 'Pass',
matcherName: 'testMatcher',
passed: false,
};
const result = expectationResultFactory(options);
expect(result.message).toMatchSnapshot();
});
});
12 changes: 8 additions & 4 deletions packages/jest-jasmine2/src/reporter.ts
Expand Up @@ -113,15 +113,19 @@ export default class Jasmine2Reporter implements Reporter {
return this._resultsPromise;
}

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

if (stack && message && !stack.includes(message)) {
return message + stack.replace(ERROR_REGEX, '\n');
if (typeof stack === 'string') {
if (stack && message && !stack.includes(message)) {
return message + stack.replace(ERROR_REGEX, '\n');
}
return stack;
} else {
return `${stack}`;
}
return stack;
}

private _extractSpecResults(
Expand Down

0 comments on commit 01486c9

Please sign in to comment.