Sinon stub function used with destructuring

Posted on

Sinon stub function used with destructuring

I wish to stub a function used in the file I’m currently testing. This function is required with a destructuring like this:

 const { theFunctionIWant } = require('path/to/module')

When testing, the stub is never called, and the real function proceed to be called.
But when I require it ‘normally’ (i.e: without destructuring)

const myModule = require('path/to/module')

then the stub is correctly used and everything works fine

I sense that it’s because of how the destructuring works and the fact that sinon stub the object property and not the function directly. Anyhow if you can provide me some insights I will be grateful !

Solution :

The reason stubbing the method of the module doesn’t work when using destructuring from a dependant module is quite simple and has to do with the time you bind the actual function reference. It doesn’t have anything to do with CommonJS modules, Sinon or Node per se, so I’ll start out with simple javascript examples.

const stub = (o, method) => (o[method] = () => "I am a stub");

const obj = {
  methodFoo() {
    return "I am foo";

// same as doing `const methodFoo = obj.methodFoo;`
const { methodFoo } = obj; // "import" using destructuring

console.log("obj.methodFoo(): ", obj.methodFoo());
console.log("methodFoo()", methodFoo());

console.log("Stubbing out method!");
stub(obj, "methodFoo");

console.log("obj.methodFoo: ", obj.methodFoo());
console.log("methodFoo()", methodFoo());

If you run the example above, you will see that even though
you have stubbed the methodFoo property of the obj “module”,
the directly bound reference still returns the old value!

This is because, when stubbing you are essentially assigning
a new value (a function) to a property of an object (here: obj). New references to this new value (using obj.methodFoo) will print the new values,
but if you have stored a reference to the old function you will
still get the old return values when calling the old function.

The same applies to your original problem. If you in module A have a dependency
on a function foo in module B and store that reference, then
it doesn’t matter if you assign a new value (for instance a stub) to
the exported value, since you already stored a reference to the old value.

In essence:

This will be affected by stubbing

const A = require('./A');

function doFoo(){; // will always evalute  A['foo']()

This will not be affected by stubbing

const myFoo = require('./A').foo;

function doFoo(){
    myFoo(); // will just evalute to the original value of

Since your module returns an object and theMethodIwant is property of that object you can define your stub like this:

const myModule = require('path/to/module')
myModule.theMethodIwant = sinon.stub()

Leave a Reply

Your email address will not be published. Required fields are marked *