diff --git a/packages/jest-jasmine2/src/__tests__/__snapshots__/expectationResultFactory.test.ts.snap b/packages/jest-jasmine2/src/__tests__/__snapshots__/expectationResultFactory.test.ts.snap index 73fd8445e1d2..f094459f5f43 100644 --- a/packages/jest-jasmine2/src/__tests__/__snapshots__/expectationResultFactory.test.ts.snap +++ b/packages/jest-jasmine2/src/__tests__/__snapshots__/expectationResultFactory.test.ts.snap @@ -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, diff --git a/packages/jest-jasmine2/src/__tests__/expectationResultFactory.test.ts b/packages/jest-jasmine2/src/__tests__/expectationResultFactory.test.ts index 59cebccb4cd9..7b00492e5edb 100644 --- a/packages/jest-jasmine2/src/__tests__/expectationResultFactory.test.ts +++ b/packages/jest-jasmine2/src/__tests__/expectationResultFactory.test.ts @@ -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(); + }); }); diff --git a/packages/jest-jasmine2/src/reporter.ts b/packages/jest-jasmine2/src/reporter.ts index 2bcb0ad79c35..9829f1db7805 100644 --- a/packages/jest-jasmine2/src/reporter.ts +++ b/packages/jest-jasmine2/src/reporter.ts @@ -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(