Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stub out module function

Edit: Being a little bit more precise.

I want to test usecases for a Github API wrapper extension, that our team has created. For testing, we don't want to use API wrapper extension directly, so we want to stub out its functions. All calls to the API wrapper should be stubbed out for the tests, not just creating a clone stub.

I have a module "github" in Node.js:

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

And I am requiring it like this:

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

Now, I would like to stub out 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 from me to pass an object and a method and does not allow me to stub out a module that is a function.

Any ideas?

like image 510
mitchkman Avatar asked Jun 17 '14 23:06

mitchkman


People also ask

How do you stub a dependency of a module?

To stub a dependency (imported module) of a module under test you have to import it explicitly in your test and stub the desired method. For the stubbing to work, the stubbed method cannot be destructured, neither in the module under test nor in the test.

What is stub method in JavaScript?

A method stub or simply stub in software development is a piece of code used to stand in for some other programming functionality. A stub may simulate the behavior of existing code (such as a procedure on a remote machine; such methods are often called mocks) or be a temporary substitute for yet-to-be-developed code.

What is concept of stub in node JS?

Stubs are functions or programs that affect the behavior of components or modules. Stubs are dummy objects for testing. Stubs implement a pre-programmed response.

What is Proxyquire?

Proxyquire is used to proxy a required module in node js. Having said that, if we require any dependency into the given file, we can proxy that with a stub, and only test code for given file. Few words about sinon. Sinon is used to create stubs, mocks, and spies in javascript.


2 Answers

There might be a way to accomplish this in pure Sinon, but I suspect it would be pretty hacky. However, proxyquire is a node library that is designed for solving these sort of issues.

Supposing you want to test some module foo that makes use of the github module; you'd write something like:

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

where myFakeGithubStub can be anything; a complete stub, or the actual implementation with a few tweaks, etc.

If, in the above example, myFakeGithubStub has a property "@global" set as true, (i.e. by executing myFakeGithubStub["@global"] = true) then the github module will be replaced with the stub not only in the foo module itself, but in any module that the foo module requires. However, as stated in the proxyquire documentation on the global option, generally speaking this feature is a sign of poorly designed unit tests and should be avoided.

like image 151
Retsam Avatar answered Sep 20 '22 11:09

Retsam


I found that this 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 e.g. '/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 have to make sure that you stub the module (as above) before it's required elsewhere...

// elsewhere...

const moduleFunction = require( 'moduleFunction' ); 

moduleFunction();    // returns 'I'm stubbed!'
like image 45
Ben Davies Avatar answered Sep 21 '22 11:09

Ben Davies