Stub out module function

后端 未结 4 1132
粉色の甜心
粉色の甜心 2021-02-13 21:07

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 wrap

相关标签:
4条回答
  • 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.

    0 讨论(0)
  • 2021-02-13 21:55

    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!'
    
    0 讨论(0)
  • 2021-02-13 21:56

    If you are doing

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

    in global scope, then you can using 'global' as the object and 'github' as the method to be stubbed out.

    var stub_github = sinon.stub(global, "github", function (args, callback) { 
        console.log("the github(...) call was stubbed out!!");
    });
    
    0 讨论(0)
  • 2021-02-13 21:58

    Simplest solution is to refactor your module:

    instead of this:

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

    do this:

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

    Now you can require it with:

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

    To stub:

    const github = require('../services/github.js');
    let githubStub = sinon.stub(github, 'github', function () {
        ... 
    });
    
    0 讨论(0)
提交回复
热议问题