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

Retain spy function names and fix spy.named(name) #1987

Merged
merged 1 commit into from Mar 4, 2019
Merged

Conversation

mantoni
Copy link
Member

@mantoni mantoni commented Mar 1, 2019

Purpose (TL;DR) - mandatory

  • Let spies have the same name as the function they wrap
  • Change the name property in spy.named(name), not just the displayName (used for sinon error messages)

Fixes #250

Background (Problem in detail)

We used to retain the name of spied functions, but this has been lost along the way at some point.

There is #950 which shows in the issue description that the name property of a spy reflected the original function name. However, the test that is verifying this was added later and it hard coded the name to "proxy".

I think this dates back to the old times when we used eval to generate the spy function.

Solution - optional

This change verifies that a name can be configured on the function – unfortunately this is inconsistent across (old but supported) browser versions – and then configures the name using the same property descritor flags as the original value.

How to verify - mandatory

  1. Check out this branch
  2. npm install
  3. npm test
  4. npm run test-cloud

Checklist for author

  • npm run lint passes
  • References to standard library functions are cached.

@mantoni mantoni added the semver:patch changes will cause a new patch version label Mar 1, 2019
@mantoni mantoni requested a review from mroderick March 1, 2019 18:01
@coveralls
Copy link

Pull Request Test Coverage Report for Build 2811

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.04%) to 94.109%

Files with Coverage Reduction New Missed Lines %
sinon/spy.js 2 89.86%
Totals Coverage Status
Change from base Build 2809: -0.04%
Covered Lines: 1657
Relevant Lines: 1725

💛 - Coveralls

@@ -152,6 +152,12 @@ function createProxy(func, proxyLength) {
return p.invoke(func, this, slice(arguments));
};
}
var nameDescriptor = Object.getOwnPropertyDescriptor(func, "name");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in this change looks very similar to the code in the next change. Would it be wise to extract it to a shared function? Perhaps even put that function in @sinonjs/commons?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I had the same thought, but in one case we're just using one descriptor as is, in the other case we modfy the value. Extracting a function produces more code and would include a superfluous value assignment for one of the cases. So I decided against it for clarity.

I am treating commons as a toolbelt for shared functions. This is a Sinon only feature so far. We can move it if we want to reuse it elsewhere.

Copy link
Member

@mroderick mroderick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THUNDERBIRDS ARE GO!

@mantoni mantoni merged commit 19b3fc7 into master Mar 4, 2019
@mantoni mantoni deleted the spy-function-name branch March 4, 2019 10:05
@mantoni mantoni mentioned this pull request Mar 4, 2019
@mantoni
Copy link
Member Author

mantoni commented Mar 4, 2019

Released in v7.2.7.

franck-romano pushed a commit to franck-romano/sinon that referenced this pull request Oct 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver:patch changes will cause a new patch version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants