mercredi 20 mai 2015

How can I use sinon to spy on a function that is imported into an ember-cli app?

I have an ember app built using ember-cli conventions and I would like to spy on a util function using sinon. The problem is I don't know how to reference the object that the function is defined on.

I have a function defined as follows in app/utils/report-error.js:

reportError = function(err) {
  # ...
};
export default reportError;

And then I import that function elsewhere as follows:

import reportError from '../utils/report-error';

Is it possible to spy on this function using sinon? Normally I would refer to the object on which the function is defined:

sandbox = sinon.sandbox.create();
sandbox.spy(someObject, "reportError");

But what is the someObject of a bare function that gets imported?

I realize I could just put the function on an object and import the object, but that means that everywhere the function is used, I need to reference the receiver object too, which seems messy.

When the I look at the javascript that is produced after the import and export statements are transpiled, there is actually an object with a function on it:

reportError['default']();

But any mentions to reportError get similarly transpiled and there doesn't seem a way to refer to the host object of the dependency.

Thoughts?




Aucun commentaire:

Enregistrer un commentaire