From 84c7e2b21bb62d448c10c112b65049eee70f2e19 Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Mon, 7 Oct 2024 20:04:07 -0700 Subject: [PATCH] Fixed de-duplication of exception messages containing blank lines on Node and Chrome This is particularly helpful when reporting testing-library errors, which have messages that contain blank lines and can be hundreds or even thousands of lines long. --- lib/jasmine-core/jasmine.js | 8 +++++--- spec/core/StackTraceSpec.js | 21 +++++++++++++++++++++ src/core/StackTrace.js | 8 +++++--- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 81d48cb3..d1a065d0 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -9889,9 +9889,7 @@ getJasmineRequireObj().SpyStrategy = function(j$) { getJasmineRequireObj().StackTrace = function(j$) { function StackTrace(error) { - let lines = error.stack.split('\n').filter(function(line) { - return line !== ''; - }); + let lines = error.stack.split('\n'); const extractResult = extractMessage(error.message, lines); @@ -9900,6 +9898,10 @@ getJasmineRequireObj().StackTrace = function(j$) { lines = extractResult.remainder; } + lines = lines.filter(function(line) { + return line !== ''; + }); + const parseResult = tryParseFrames(lines); this.frames = parseResult.frames; this.style = parseResult.style; diff --git a/spec/core/StackTraceSpec.js b/spec/core/StackTraceSpec.js index b294bd6c..2f94799d 100644 --- a/spec/core/StackTraceSpec.js +++ b/spec/core/StackTraceSpec.js @@ -51,6 +51,27 @@ describe('StackTrace', function() { ]); }); + it('understands Chrome/Edge style traces with messages containing blank lines', function() { + const error = { + message: 'line 1\n\nline 2', + stack: + 'Error: line 1\n\nline 2\n' + + ' at UserContext. (http://localhost:8888/__spec__/core/UtilSpec.js:115:19)\n' + + ' at QueueRunner.run (http://localhost:8888/__jasmine__/jasmine.js:4320:20)' + }; + + const result = new jasmineUnderTest.StackTrace(error); + + expect(result.message).toEqual('Error: line 1\n\nline 2'); + const rawFrames = result.frames.map(function(f) { + return f.raw; + }); + expect(rawFrames).toEqual([ + ' at UserContext. (http://localhost:8888/__spec__/core/UtilSpec.js:115:19)', + ' at QueueRunner.run (http://localhost:8888/__jasmine__/jasmine.js:4320:20)' + ]); + }); + it('understands Node style traces', function() { const error = { message: 'nope', diff --git a/src/core/StackTrace.js b/src/core/StackTrace.js index 9038e470..dfefca6a 100644 --- a/src/core/StackTrace.js +++ b/src/core/StackTrace.js @@ -1,8 +1,6 @@ getJasmineRequireObj().StackTrace = function(j$) { function StackTrace(error) { - let lines = error.stack.split('\n').filter(function(line) { - return line !== ''; - }); + let lines = error.stack.split('\n'); const extractResult = extractMessage(error.message, lines); @@ -11,6 +9,10 @@ getJasmineRequireObj().StackTrace = function(j$) { lines = extractResult.remainder; } + lines = lines.filter(function(line) { + return line !== ''; + }); + const parseResult = tryParseFrames(lines); this.frames = parseResult.frames; this.style = parseResult.style;