How to mock a function from module

José Castro
Devsys
Published in
1 min readJun 26, 2017

If you have something like:

var myFunction = function(...){
...
};
var myFunction2 = function(...){
myFunction();
};
exports.myFunction = myFunction;
exports.myFunction2 = myFunction2;

Then you try testing myFunction2 with a mock of myFunction, but you can’t, because mocking a function after requiring a module is impossible in javascript.

So I read this possible solution

var myFunction = function(...){
...
};
var myFunction2 = function(...){
exports.myFunction();
};
exports.myFunction = myFunction;
exports.myFunction2 = myFunction2;

And then in your test:

module.myFunction = function(...){ ... };
module.myFunction2(...);

But for me still I get the initial function myFunction.

So I try to use rewire from the repo:

rewire acts exactly like require. With just one difference: Your module will now export a special setter and getter for private variables.myModule.__set__("path", "/dev/null");
myModule.__get__("path"); // = "/dev/null"
This allows you to mock everything in the top-level scope of the module, like the fs module for example. Just pass the variable name as first parameter and your mock as second.

So I use this to mock the module function like this:

var rewire = require('rewire');
var module = rewire('../module');
module.__set__('myFunction', function(...){ console.log('do anything!'); });

And now I can mock and set the behaviour for the test! :)

--

--