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?

1 Answer 1

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;        
});
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the suggestion! i will try this out and let you know if it worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.