Skip to content

CallTracker wrapper function to have original function length #40484

Closed
@primeare

Description

@primeare

Is your feature request related to a problem?

Some frameworks and libraries (ex. Express.js middleware execution) rely on the function length and use it to decide the appropriate behaviour.

Currently, the CallTracker Node.js API does not proxy the original function length. Thus, its application may be limited in certain circumstances.

import assert from 'assert';

function originalFn(a, b, c) { /* noop */ }

const tracker = new assert.CallTracker();
const trackedFn = tracker.calls(originalFn, 1);

console.log(`originalFn length: ${originalFn.length}`); // => 3
console.log(`trackedFn length: ${trackedFn.length}`); // => 0

Such widely popular test automation frameworks as Jest have built-in functionality to match the arity of a function when it is mocked. And they also consider the original function length as a source of truth.

So here, it is proposed to change the CallTracker.calls wrapper function to match the arity of the original function.
I would be glad to work on this feature and create my first code contribution to the Node.js codebase! 😃

Describe the solution you'd like

To achieve the goal of this feature request, it is proposed to change the CallTracker.calls wrapper function, and, for instance, redefine the length property of this function with:

ReflectDefineProperty(wrapperFunction, 'length', { value: originalFn.length });

Moreover, the wrapper function may also be refactored to use rest parameters instead of the arguments object.

Describe alternatives you've considered

It is an open question whether it is required to proxy call, apply and bind methods calls to the original function, as well as to detect if the wrapper function is called as a constructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions