Rollup and Sinon not playing nice together. Referencing the incorrect function.
I'm trying to use Sinon and Rollup to test some function calls. The problem is that the sinon spy that i'm creating:
const spy = sinon.spy(method, 'function');
that I'm using, when generated by rollup, doesn't reference the correct thing. I have listed both my code and the rolled up code:
Mine:
import assert from 'assert';
import sinon from 'sinon';
import * as playbackModule from '../components/playback';
describe('App', () => {
it('initialises the app', () => {
const spy = sinon.spy(playbackModule, 'playback');
playbackModule.playback();
assert.equal(spy.called, true);
});
});
Generated:
function playback() {
console.log('called');
}
var playbackModule = {
playback: playback
};
describe('App', function () {
it('initialises the app', function () {
var spy = sinon_1.spy(playbackModule, 'playback');
playback();
assert_1.equal(spy.called, true);
});
});
As you can see, the reference for playback() is not the one in the object, therefore the spy will never say that it was called.
Is there any way, using this setup (sinon and rollup) that I can get the spy's correctly referencing the right thing? Interestingly, in my code, if I replace playbackModule.playback(); with playbackModule['playback'](); it correctly gives me called = true;
Am curious about what version you're using and your configuration? I'd expect it to look like this instead...
var playbackModule = Object.freeze({
playback: playback
});
...in which case attempting to spy on the method would fail. (This is correct before AFAICT, because playbackModule is a namespace and not an object, in the traditional sense.)
I think you have two options — spy on the function itself...
var playbackSpy = sinon.spy(playbackModule.playback);
playbackSpy();
assert.equal(playbackSpy.called, true);
...or create a new object:
var functions = {
playback: playbackModule.playback
};
// later
var spy = sinon.spy( functions, 'playback' );
functions.playback();
assert.equal( spy.called, true );
@Rich-Harris Apologies, we're using a plugin that strips out Object.freeze. You are right, by default it does look like you expected.
The first option doesn't work as the sinon.spy(playbackModule.playback) will return the spy that then you pass in to be called. What we actually want to do is check down the call stack that when calling app() it then calls playback(). Essentially, we want to know that calling X, calls Y.
The second suggestion still doesn't reference the correct thing in the following example which is closer to our usecase: rollup example - at this point, the playback function is not the same thing we're attempting to check is called.
app.js is just referencing the function, so it doesn't really matter what you do to the namespace object. The only way you could spy on the playback function is if components/playback.js exported a writable object. But then the tail is wagging the dog.
That's exactly why this issue was raised, and I was hoping there was another way other than exporting an object on the default, as you obviously lose the benefits of tree shaking.
The only other solution I have thought about is using DI, which wont work in all scenarios; especially if you have a util that's imported in multiple places (and therefore cannot possibly stub/spy at all) - so is not a viable options.
I'm assuming there are no other ways around this. I'm also surprised at the lack of information around this (not here, just in general) - I'd of assumed this was a common use case - but perhaps not?
edit:
As a side note, webpack does work in this use case, which is why I assumed it was indeed an issue that should be raised (we want to use rollup over webpack for it's smaller footprint).
It does seem a bit unsolvable. I think this is one of those cases where CommonJS tricked us into thinking (and building software) a certain way, because the limitations of the language pre-modules meant things had to be done in a more hackable way (dynamic requires, module.exports is an object, etc) which created opportunities for things like sinon.spy.
Personally I haven't found this to be a limitation because I only really test the public API — I don't care about the implementation details as long as the observed outputs and effects are correct for a given set of inputs. So I'm not practiced at thinking about these problems. Would be interested to hear from anyone else who has dealt with these issues though!
FWIW, I ran into the scenario that @Rich-Harris alluded to with the object being frozen preventing me from performing sinon mocking, stubbing, etc. on its methods. I generally agree with the idea of only testing the public API, or using DI to inject test objects in testing scenarios, but it's not always feasible or reasonable to expect an entire codebase to be structured this way.
In my case, I'm working on small enough of a project that I should be able to restructure with DI in mind. I'm curious to know how this pans out though. Interesting point you make about CommonJS et al. trickery leading us to this point.
I think it's safe to mark this as a wontfix as this isn't supported for ES modules in Node or browsers.
I agree.
On 25 Apr 2018, at 7:45 pm, Guy Bedford [email protected] wrote:
I think it's safe to mark this as a wontfix as this isn't supported for ES modules in Node or browsers.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
It seems that rollup exports with configurable: true in the descriptor (@guybedford ?).
If so, you can stub like so:
Object.defineProperty(logger, 'createLogger', {
writable: true,
value: sinon.stub().returns(loggerStub)
});
See this for more.
I also think Sinon could support this in the future without any extra workarounds.