Stub out module function

后端 未结 4 1148
粉色の甜心
粉色の甜心 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条回答
  •  我寻月下人不归
    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 () {
        ... 
    });
    

提交回复
热议问题