Skip to content

CallTracker wrapper function to have original function length #40484

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.

Activity

  1. added
    assertIssues and PRs related to the assert subsystem.
    feature requestIssues requesting new Node.js features.
    good first issueIssues that are suitable for first-time contributors.
    and removed
    good first issueIssues that are suitable for first-time contributors.
    on Oct 16, 2021
  2. Mesteery commented on Oct 16, 2021

    @Mesteery
    Contributor

    @primeare Would you like to work on this?

  3. primeare commented on Oct 16, 2021

    @primeare
    Author

    @Mesteery, yes, I do want to work on this feature implementation. Is there anything important or particular I should know prior to starting working on the implementation except accepting the Code of Conduct and reading the Contribution Guidelines?

  4. aduh95 commented on Oct 16, 2021

    @aduh95
    Contributor

    Is there anything important or particular I should know

    Hopefully documentation (such as in doc/guides) should contain all the info you need, but don't hesitate to ask questions if you get stuck. Don't forget to run make lint-js-fix before committing, and to add a test that fails with the current master and passes with the fix.

  5. moved this to Pending Triage in Node.js feature requestson Apr 4, 2022
  6. github-actions commented on Apr 15, 2022

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

    For more information on how the project manages feature requests, please consult the feature request management document.

  7. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Apr 15, 2022
  8. moved this from Pending Triage to Stale in Node.js feature requestson Apr 15, 2022
  9. Y1D7NG commented on Apr 28, 2022

    @Y1D7NG
    Contributor

    see this github-actions hint will close this in 6 months, can I work on this? @aduh95 @Mesteery

  10. aduh95 commented on Apr 28, 2022

    @aduh95
    Contributor

    can I work on this?

    Sure, go for it :)

  11. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Apr 29, 2022
  12. Y1D7NG commented on Apr 29, 2022

    @Y1D7NG
    Contributor

    can I work on this?

    Sure, go for it :)

    @aduh95 Thanks, I have submitted the pr #42909

  13. moved this from Stale to Done in Node.js feature requestson Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions