How to sinon spy module export utility functions

痞子三分冷 提交于 2019-12-05 22:53:19

You can use rewire module. Here is an example:

Source code:

function someFunction1() {
  console.log('someFunction1 called')
  someFunction2();
}

function someFunction2() {
  console.log('someFunction2 called')
}

module.exports = {
  someFunction1: someFunction1,
  someFunction2: someFunction2
}

Test case:

'use strict';

var expect = require('chai').expect;
var rewire = require('rewire');
var sinon = require('sinon');

var funcs = rewire('../lib/someFunctions');

it('should call the someFunction2', () => {
  var someFunction2Stub = sinon.stub();
  funcs.__set__({
    someFunction2: someFunction2Stub,
  });

  someFunction2Stub.returns(null);

  funcs.someFunction1();

  expect(someFunction2Stub.calledOnce).to.equal(true);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!