2

I have a question on using proxyquire (or any other suggestions on how to test the following code)

If i have the following file to test:

var path = require('path');

module.exports = function (conf) {
    var exported = {};

    exported.getIssue = function (issueId, done) {
        ...

        ...
    };

    return exported;
};

How do i pass in the 'conf' variable while using proxyquire to mock 'path; var?

Is there any other way to do this if not using proxyquire?

Rookie
  • 5,179
  • 13
  • 41
  • 65

1 Answers1

1

You simply have to pass conf variable to the module/function required via proxyquire. Proxyquire has meaning to to do the same CommonJs require stuff, but with the possibility of mocking and stubing some modules. So you should act the same.

var pathMock = {
   someMethod = function() {}  
};
var confMock = {
   someMethod = function() {}
};
spyOn(pathMock, 'someMethod').and.callThrough();
spyOn(confMock, 'someMethod').and.callThrough();

module   = proxyquire('../path/to/module', {
    path: pathMock
});

it('do some test', function(done) {
    module(conf).getIssue();
    expect(pathMock.someMethod).toHaveBeenCalled;
    expect(confMock.someMethod).toHaveBeenCalled;        
});
Alexandr Lazarev
  • 12,554
  • 4
  • 38
  • 47