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

CallTracker wrapper function to have original function length #40484

Closed
primeare opened this issue Oct 16, 2021 · 7 comments · Fixed by #42909
Closed

CallTracker wrapper function to have original function length #40484

primeare opened this issue Oct 16, 2021 · 7 comments · Fixed by #42909
Labels
assert Issues and PRs related to the assert subsystem. feature request Issues that request new features to be added to Node.js.

Comments

@primeare
Copy link

primeare commented Oct 16, 2021

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.

@Mesteery Mesteery added assert Issues and PRs related to the assert subsystem. feature request Issues that request new features to be added to Node.js. good first issue Issues that are suitable for first-time contributors. and removed good first issue Issues that are suitable for first-time contributors. labels Oct 16, 2021
@Mesteery
Copy link
Contributor

@primeare Would you like to work on this?

@primeare
Copy link
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?

@aduh95
Copy link
Contributor

aduh95 commented Oct 16, 2021

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.

@github-actions
Copy link
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.

@github-actions github-actions bot added the stale label Apr 15, 2022
@y1d7ng
Copy link
Contributor

y1d7ng commented Apr 28, 2022

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

@aduh95
Copy link
Contributor

aduh95 commented Apr 28, 2022

can I work on this?

Sure, go for it :)

@y1d7ng
Copy link
Contributor

y1d7ng commented Apr 29, 2022

can I work on this?

Sure, go for it :)

@aduh95 Thanks, I have submitted the pr #42909

nodejs-github-bot pushed a commit that referenced this issue May 6, 2022
PR-URL: #42909
Fixes: #40484
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
RafaelGSS pushed a commit that referenced this issue May 10, 2022
PR-URL: #42909
Fixes: #40484
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
juanarbol pushed a commit that referenced this issue May 31, 2022
PR-URL: #42909
Fixes: #40484
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
danielleadams pushed a commit that referenced this issue Jun 27, 2022
PR-URL: #42909
Fixes: #40484
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
targos pushed a commit that referenced this issue Jul 12, 2022
PR-URL: #42909
Fixes: #40484
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
targos pushed a commit that referenced this issue Jul 31, 2022
PR-URL: #42909
Fixes: #40484
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
guangwong pushed a commit to noslate-project/node that referenced this issue Oct 10, 2022
PR-URL: nodejs/node#42909
Fixes: nodejs/node#40484
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assert Issues and PRs related to the assert subsystem. feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants