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

assert: fix CallTracker calls wraps the function causes the original … #42909

Merged
merged 5 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions lib/internal/assert/calltracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
ArrayPrototypePush,
Error,
FunctionPrototype,
ObjectDefineProperty,
ReflectApply,
SafeSet,
} = primordials;
Expand Down Expand Up @@ -46,7 +47,7 @@ class CallTracker {
const callChecks = this.#callChecks;
callChecks.add(context);

return function() {
function callsFn() {
context.actual++;
if (context.actual === context.exact) {
// Once function has reached its call count remove it from
Expand All @@ -59,7 +60,13 @@ class CallTracker {
callChecks.add(context);
}
return ReflectApply(fn, this, arguments);
};
}

ObjectDefineProperty(callsFn, 'length', {
value: fn.length
y1d7ng marked this conversation as resolved.
Show resolved Hide resolved
});

return callsFn;
}

report() {
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-assert-calltracker-calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,17 @@ assert.throws(
callsNoop();
tracker.verify();
}

{
function func() {}
const tracker = new assert.CallTracker();
const callsfunc = tracker.calls(func);
assert.strictEqual(func.length, callsfunc.length);
y1d7ng marked this conversation as resolved.
Show resolved Hide resolved
}

{
function func(a, b, c = 2) {}
const tracker = new assert.CallTracker();
const callsfunc = tracker.calls(func);
assert.strictEqual(func.length, callsfunc.length);
y1d7ng marked this conversation as resolved.
Show resolved Hide resolved
}
y1d7ng marked this conversation as resolved.
Show resolved Hide resolved