Skip to content

Commit

Permalink
assert: fix CallTracker calls wraps the function causes the original …
Browse files Browse the repository at this point in the history
…length to be lost
  • Loading branch information
y1d7ng committed Apr 29, 2022
1 parent 17826f5 commit 341a56c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
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
});

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);
}

{
function func(a, b, c = 2) {}
const tracker = new assert.CallTracker();
const callsfunc = tracker.calls(func);
assert.strictEqual(func.length, callsfunc.length);
}

0 comments on commit 341a56c

Please sign in to comment.