-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[Bug]: rewrite-stack-trace logic break stack trace for Jest #14927
Description
💻
- Would you like to work on a fix?
How are you using Babel?
Programmatic API (babel.transform, babel.parse)
Input code
Steps for reproducing are described here (jestjs/jest#13248); confirmed by jest team as upstream (babel) issue. More detail on confirming the problem described below.
In
rewrite-stack-trace.ts, the Error.stackTraceLimit is being adjusted in a singleton function. This runs just once within a process.What's happening is as follows:
- Jest starts and sets
Error.stackTraceLimitto100.- At some point the babel rewrite code runs and sets
Error.stackTraceLimittoError.stackTraceLimit += STACK_TRACE_LIMIT_DELTA;(const STACK_TRACE_LIMIT_DELTA = 100;).- When errors are reported on first run, they are truncated to
newTrace.slice(0, Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA),(see here).- When a process is re-used, jest re-initializes the
Error.stackTraceLimitto100.- Because
rewrite-stack-trace.tsoperates as a singleton, it is not expectingError.stackTraceLimitto have been reset, and then completely truncates subsequent stack traces because nowError.stackTraceLimitis100, andSTACK_TRACE_LIMIT_DELTAis100, and the processing codenewTrace.slice(0, Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA)becomesnewTrace.slice(0, 100 - 100).
You can confirm this behavior by changing setupPrepareStackTrace method in distributed @babel/core/lib/errors/rewrite-stack-trace.js:
...
function setupPrepareStackTrace() {
+ require('fs').appendFileSync('path_to_log_file.txt', (Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA).toString() + '\n');
return prepareStackTrace(err, newTrace.slice(0, Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA));
...
}
...Sample output when running the sample repo provided in the jest issue, for Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA:
100
100
100
100
0
After this point, the error stack is always completely truncated.
Configuration file name
No response
Configuration
Default Jest Babel Configuration (not applicable to the issue)
Current and expected behavior
Babel should be tolerant of other tooling setting Error.stackTraceLimit.
Environment
@babel/core@7.19.0
Possible solution
No response
Additional context
No response