From aa36521409615de50e7d0690a0e65862f249cc3a Mon Sep 17 00:00:00 2001 From: RedYetiDev <38299977+RedYetiDev@users.noreply.github.com> Date: Thu, 5 Sep 2024 16:15:20 -0400 Subject: [PATCH] assert: allow `getErrMessage` to check deeper into the stacktrace --- lib/assert.js | 20 +++++++++++++++++--- test/parallel/test-runner-assert.js | 4 ++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index eadc3844c20128..2b5a6c5705741a 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -282,12 +282,26 @@ function parseCode(code, offset) { throw null; } +function getInvokingCall(stack) { + // Try to get the userland function that invoked this assertion + for (let i = 0; i < stack.length; i++) { + const call = stack[i]; + const filename = call.getFileName(); + if (StringPrototypeStartsWith(filename, 'node:') && BuiltinModule.exists(StringPrototypeSlice(filename, 5))) { + continue; + } else return call; + } + // If that's not possible, just return the function that did. + return stack[0]; +} + function getErrMessage(message, fn) { const tmpLimit = Error.stackTraceLimit; const errorStackTraceLimitIsWritable = isErrorStackTraceLimitWritable(); // Make sure the limit is set to 1. Otherwise it could fail (<= 0) or it - // does to much work. - if (errorStackTraceLimitIsWritable) Error.stackTraceLimit = 1; + // does to much work. The limit is set to 2, as this allows any wrappers + // around this assertion function to be skipped. + if (errorStackTraceLimitIsWritable) Error.stackTraceLimit = 2; // We only need the stack trace. To minimize the overhead use an object // instead of an error. const err = {}; @@ -295,7 +309,7 @@ function getErrMessage(message, fn) { if (errorStackTraceLimitIsWritable) Error.stackTraceLimit = tmpLimit; overrideStackTrace.set(err, (_, stack) => stack); - const call = err.stack[0]; + const call = getInvokingCall(err.stack); let filename = call.getFileName(); const line = call.getLineNumber() - 1; diff --git a/test/parallel/test-runner-assert.js b/test/parallel/test-runner-assert.js index 2af05c95414d79..51c6ee67ebe4d3 100644 --- a/test/parallel/test-runner-assert.js +++ b/test/parallel/test-runner-assert.js @@ -24,3 +24,7 @@ test('only methods from node:assert are on t.assert', (t) => { 'throws', ]); }); + +test('t.assert.ok correctly parses the stacktrace', (t) => { + t.assert.throws(() => t.assert.ok(1 === 2), /t\.assert\.ok\(1 === 2\)/); +});