Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stack.slice not a function #828

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/core/src/util/stackTrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ exports.captureStackTrace = function captureStackTrace(length, referenceFunction
// drawback is that it might show too much, but that's better than not having any stack trace at all.
Error.captureStackTrace(stackTraceTarget);
}
const stack = stackTraceTarget.stack;

let stack = stackTraceTarget.stack;

if (!Array.isArray(stack)) {
stack = [];
}

Error.stackTraceLimit = originalLimit;
Error.prepareStackTrace = originalPrepareStackTrace;

if (drop > 0) {
if (drop > 0 && stack.length >= drop) {
stack.splice(0, drop);
}
return stack;
Expand Down
29 changes: 29 additions & 0 deletions packages/core/test/util/stackTrace_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,33 @@ describe('util/stackTrace', () => {
expect(stack[1].m).to.equal('c');
expect(stack).to.have.lengthOf(2);
});

it('must not capture stack type string', () => {
let stack;

(function a() {
const stackTraceTarget = { stack: 'this is not an array' };
const orig = Error.captureStackTrace;
Error.captureStackTrace = target => {
Object.assign(target, stackTraceTarget);
};

stack = stackTrace.captureStackTrace(2, a);
Error.captureStackTrace = orig;
})();

expect(stack.length).to.equal(0);
});

it('must capture stack length < drop', () => {
const stackTraceTarget = { stack: new Array(5) };
const orig = Error.captureStackTrace;
Error.captureStackTrace = target => {
Object.assign(target, stackTraceTarget);
};

const stack = stackTrace.captureStackTrace(2, this, 10);
Error.captureStackTrace = orig;
expect(stack.length).to.equal(5);
});
});