Stub Module Function

Edit: Be a little more accurate.

I want to test usecases to extend the Github API shell created by our team. For testing, we don’t want to use the extension of the API shell directly, therefore we want to limit its functions. All calls to the API shell should be skipped for tests, and not just for creating a stub.

I have a github module in Node.js:

module.exports = function(args, done) { ... } 

And I demand it like this:

 var github = require('../services/github'); 

Now I would like to github(...) using Sinon.js:

 var stub_github = sinon.stub(???, "github", function (args, callback) { console.log("the github(...) call was stubbed out!!"); }); 

But sinon.stub(...) expects me to pass an object and method and does not allow me to drown out the module, which is a function.

Any ideas?

+7
javascript sinon
source share
4 answers

There may be a way to do this in pure Sinon, but I suspect it will be pretty hacks. However, proxyquire is a node library designed to solve such problems.

Suppose you want to test some foo module that uses the github module; you should write something like:

 var proxyquire = require("proxyquire"); var foo = proxyquire(".foo", {"./github", myFakeGithubStub}); 

where myFakeGithubStub can be anything; full stub or actual implementation with a few settings, etc.

If in the example above myFakeGithubStub has the property "@global" set to true (that is, by executing myFakeGithubStub["@global"] = true ), then the github module will be replaced by a stub not only in the foo module itself, but in any the module that the foo module requires. However, as indicated in the proxyquire documentation for the global parameter , this function is usually a sign of poorly developed unit tests and should be avoided.

+8
source share

I found that it worked for me ...

 const sinon = require( 'sinon' ); const moduleFunction = require( 'moduleFunction' ); // Required modules get added require.cache. // The property name of the object containing the module in require.cache is // the fully qualified path of the module eg '/Users/Bill/project/node_modules/moduleFunction/index.js' // You can get the fully qualified path of a module from require.resolve // The reference to the module itself is the exports property const stubbedModule = sinon.stub( require.cache[ require.resolve( 'moduleFunction' ) ], 'exports', () => { // this function will replace the module return 'I\'m stubbed!'; }); // sidenote - stubbedModule.default references the original module... 

You must ensure that you plug the module (as described above) before you need it elsewhere ...

 // elsewhere... const moduleFunction = require( 'moduleFunction' ); moduleFunction(); // returns 'I'm stubbed!' 
+3
source share

The simplest solution is to reorganize your module:

instead of this:

 module.exports = function(args, done) { ... } 

do the following:

 module.exports = function(){ return module.exports.github.apply(this, arguments); }; module.exports.github = github; function github(args, done) { ... } 

Now you can request it with:

 const github = require('../services/github.js'); //or const github = require('../services/github.js').github; 

Block:

 const github = require('../services/github.js'); let githubStub = sinon.stub(github, 'github', function () { ... }); 
+3
source share

If you do

var github = require('../services/github');

in a global scope, then you can use global as an object and github as a method to be deleted.

 var stub_github = sinon.stub(global, "github", function (args, callback) { console.log("the github(...) call was stubbed out!!"); }); 
-2
source share

All Articles