Sinon: Named spies

Created on 25 Feb 2013  ·  13Comments  ·  Source: sinonjs/sinon

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.

Help wanted

Most helpful comment

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.

All 13 comments

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

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

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?

// 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

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.

I got around this through the following:

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

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

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!

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.

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

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.

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NathanHazout picture NathanHazout  ·  3Comments

brettz9 picture brettz9  ·  3Comments

zimtsui picture zimtsui  ·  3Comments

kevinburkeshyp picture kevinburkeshyp  ·  4Comments

ndhoule picture ndhoule  ·  4Comments