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

Named spies #250

Closed
tekumara opened this issue Feb 25, 2013 · 13 comments
Closed

Named spies #250

tekumara opened this issue Feb 25, 2013 · 13 comments

Comments

@tekumara
Copy link

It would help with readability of assertion error messages if spies (other than the object property ones) could be created with names. For example, Jasmine spies allow this.

@mantoni
Copy link
Member

mantoni commented Feb 25, 2013

Sinon inspects the wrapped function name. So you can name the spy by wrapping an empty named function.

var spy = sinon.spy(function doStuff() {});

This technique also allows you to produce a function with a specific length:

var spy = sinon.spy(function doStuff(a, b, c) {}); // spy.length === 3

@netpoetica
Copy link

As far as I can see, it's always "proxy" as a function name. Are you sure this comment is true?

@mroderick
Copy link
Member

As far as I can see, it's always "proxy" as a function name. Are you sure this comment is true?

Would you mind posting a runnable example?

@netpoetica
Copy link

netpoetica commented Jan 27, 2017

// test/index.js
var sinon = require('sinon');

var spy = sinon.spy(function doStuff() {
	console.log('Name: ' + spy.name);
});

spy();
➜  sinon-test node test/index.js 
Name: proxy

I can't remember what I did before, but I was getting Mocha output from failing tests where the function name was "proxy" and you wouldn't know it was "doStuff". If I can remember what exactly what it was, I will post

@ljharb
Copy link
Contributor

ljharb commented Jul 8, 2018

I'm seeing this; it always says the name is proxy - but if i Object.defineProperty(spy, 'name', { value: 'new name' }) then it reports "new name".

It'd be great to reopen this.

@Tbhesswebber
Copy link

I got around this through the following:

const mapMethod = sinon.spy(Array.prototype, 'map');
const methodName = map.wrappedMethod.name;
console.log(methodName); // 'map'

@ljharb
Copy link
Contributor

ljharb commented Feb 20, 2019

@Tbhesswebber that doesn't work when non-test code is relying on the function's name.

@Tbhesswebber
Copy link

Ah... excellent point. I'm working on curricula for teaching higher-order functions (using Array.prototype as the starting point), which is what lead me to this issue. Very different use-case!

P.S. - testing implementation details is really tedious when you're actually trying to!

@mroderick
Copy link
Member

I can see how this would be useful. If anyone wants to dig deeper into this, that would be cool.

It might be that there are features or tests that depend on the name of the returned function. If that's the case, we should discuss how to get around it.

@ljharb
Copy link
Contributor

ljharb commented Feb 28, 2019

@mroderick when creating a spy with an original, Object.defineProperty(spy, 'name', { configurable: true, value: spy.wrappedMethod.name }) should do it?

@mantoni
Copy link
Member

mantoni commented Feb 28, 2019

I think this used to be a feature we had – naming the spy by giving the function a name – guess it got lost on the way. I’m fine with the proposal to allow naming it explicitly. But shouldn’t we also change the „proxy“ name? I feel like a spied function should look as close as possible like the original one. We ensure the correct arity, but we change the name now. Seems wrong.

@mantoni
Copy link
Member

mantoni commented Mar 1, 2019

I just realized that we also have spy.named(name) in the API 😆 It sets a displayName property on the spy. It should also change the functions name, I think. Having a look at this now.

@mantoni
Copy link
Member

mantoni commented Mar 4, 2019

The patch from #1987 was released in v7.2.7. Please let us known if this is satisfactory for everyone ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants