From 01486c93e7209ff69dfc91573f72c94a44a7fbef Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 25 Oct 2020 11:06:17 +0530 Subject: [PATCH] fix(jest-jasmine2): handle case when stack is not string --- .../expectationResultFactory.test.ts.snap | 6 ++++++ .../src/__tests__/expectationResultFactory.test.ts | 12 ++++++++++++ packages/jest-jasmine2/src/reporter.ts | 12 ++++++++---- 3 files changed, 26 insertions(+), 4 deletions(-) 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(