-
-
Notifications
You must be signed in to change notification settings - Fork 161
fix(runtime): mock declared prototype methods #9913
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
Closed
proggeramlug
wants to merge
1
commit into
PerryTS:main
from
proggeramlug:fix/9202-test-mock-prototype
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `node:test`'s `mock.method()` now replaces declared class prototype methods for instance dispatch and restores their original behavior, while recording calls and receiver identity like Node. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🤖 get_repo_knowledge executed:
get_repo_knowledge PerryTS/perry /tmp/coderabbit-repo-knowledge/perryts-perry-d4a878bc/learnings /tmp/coderabbit-repo-knowledge/perryts-perry-d4a878bc/conventionsLength of output: 21123
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 50370
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 50370
🌐 Web query:
Node.js 26.5.1 node:test mock.method empty string method name prototype💡 Result:
In Node.js's node:test module, the mock.method(object, methodName) function requires the methodName argument to be a valid, existing method name on the provided object [1][2][3]. Providing an empty string ('') as the methodName will cause the function to fail because it attempts to look up that property on the object [4][2]. Specifically: 1. Validation: The mock.method function validates that the methodName is a string or symbol [4]. If the provided name does not correspond to an actual function existing on the object (or its prototype chain), or if the lookup fails, Node.js will throw an error (typically TypeError: The argument 'methodName' must be a method) [5][2][3]. 2. Prototype Chain: The mock.method API is designed to support mocking methods defined on the object itself as well as those found on its prototype chain [4][6]. If you intend to mock a prototype method, you should pass the prototype object directly as the first argument, for example: mock.method(MyClass.prototype, 'methodName') [5][4]. 3. Empty String Behavior: An empty string is not a valid method name in standard JavaScript objects unless a method is explicitly defined with that key (e.g., obj[''] = => {}). If no such method exists, passing an empty string will result in a validation or execution error, as the mock tracker cannot identify it as a valid function to replace [2][3]. To successfully mock a method, ensure the second argument matches the exact key of the function you wish to mock [2][3]. If you are working with classes and struggling to target instance methods, ensure you are passing the prototype if you want to mock all instances, or the specific instance if you only want to mock that one [4][6].
Citations:
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 45453
Handle empty prototype method names.
When a prototype contains a callable property keyed by
"",set_method_property_valuepassesname_len == 0tojs_register_prototype_method, which returns without registering the wrapper. Instance calls can then bypass the mock. Support zero-length names or preserve prototype-registry dispatch through another path. Add a regression test and run runtime tests withRUST_TEST_THREADS=1.🤖 Prompt for AI Agents