@@ -282,20 +282,34 @@ function parseCode(code, offset) {
282282 throw null ;
283283}
284284
285+ function getInvokingCall ( stack ) {
286+ // Try to get the userland function that invoked this assertion
287+ for ( let i = 0 ; i < stack . length ; i ++ ) {
288+ const call = stack [ i ] ;
289+ const filename = call . getFileName ( ) ;
290+ if ( StringPrototypeStartsWith ( filename , 'node:' ) && BuiltinModule . exists ( StringPrototypeSlice ( filename , 5 ) ) ) {
291+ continue ;
292+ } else return call ;
293+ }
294+ // If that's not possible, just return the function that did.
295+ return stack [ 0 ] ;
296+ }
297+
285298function getErrMessage ( message , fn ) {
286299 const tmpLimit = Error . stackTraceLimit ;
287300 const errorStackTraceLimitIsWritable = isErrorStackTraceLimitWritable ( ) ;
288301 // Make sure the limit is set to 1. Otherwise it could fail (<= 0) or it
289- // does to much work.
290- if ( errorStackTraceLimitIsWritable ) Error . stackTraceLimit = 1 ;
302+ // does to much work. The limit is set to 2, as this allows any wrappers
303+ // around this assertion function to be skipped.
304+ if ( errorStackTraceLimitIsWritable ) Error . stackTraceLimit = 2 ;
291305 // We only need the stack trace. To minimize the overhead use an object
292306 // instead of an error.
293307 const err = { } ;
294308 ErrorCaptureStackTrace ( err , fn ) ;
295309 if ( errorStackTraceLimitIsWritable ) Error . stackTraceLimit = tmpLimit ;
296310
297311 overrideStackTrace . set ( err , ( _ , stack ) => stack ) ;
298- const call = err . stack [ 0 ] ;
312+ const call = getInvokingCall ( err . stack ) ;
299313
300314 let filename = call . getFileName ( ) ;
301315 const line = call . getLineNumber ( ) - 1 ;
@@ -305,10 +319,7 @@ function getErrMessage(message, fn) {
305319
306320 if ( filename ) {
307321 identifier = `${ filename } ${ line } ${ column } ` ;
308-
309- // Skip Node.js modules!
310- if ( StringPrototypeStartsWith ( filename , 'node:' ) &&
311- BuiltinModule . exists ( StringPrototypeSlice ( filename , 5 ) ) ) {
322+ if ( StringPrototypeStartsWith ( filename , 'node:' ) && BuiltinModule . exists ( StringPrototypeSlice ( filename , 5 ) ) ) {
312323 errorCache . set ( identifier , undefined ) ;
313324 return ;
314325 }
0 commit comments