IonicaBizau / node-ipinfo

:globe_with_meridians: An http://ipinfo.io NodeJS wrapper.
MIT License
57 stars 24 forks source link

Unit testing with ipInfo #5

Closed souly1 closed 8 years ago

souly1 commented 8 years ago

Hi Ionică Bizău, great module. I'm having trouble stubbing ipinfo during unit tests using mocha+sinon+proxyquire, I presume it mainly due it not returning an object during require. Tried to follow some explanations on proxy quire page for how to test the module, but in vane. any recommendations?

Thanks

IonicaBizau commented 8 years ago

Are you trying to redirect somehow the request to not make the http request to ipinfo.io but to localhost?

If so, you can override the IpInfo.HOSTNAME during the tests.

I'm not sure if this is what you need, tho. :thought_balloon:

souly1 commented 8 years ago

Hi, thanks for the reply. Basically what I need is when code attempts to run ip info in this manner: ipinfo(ip, function (err, cLoc) {}); the code in 'node_modules/ipinfo/lib/index' be run but instead mine will be run, basically returning my wanted err or cLoc, and with Sinon's ability, will be able to notify if code indeed reached at all or not using mock.called() which returns true if indeed function called. the later ability, with Sinon, is less important as one can simulate the behaviour with a simple flag in the mock implementation of ipinfo. Hope this helps.

Thanks

IonicaBizau commented 8 years ago

Ah, I see what you mean.

Yes, probably because it's exporting a function, proxyquire can't override it. But like I mentioned before, you can override the HOSTNAME field to a localhost server where you send the custom response.

I would recommend opening an issue in proxyquire to double check if overriding the module behavior is possible. Not quite sure.

souly1 commented 8 years ago

OK, give an update here if you discover a solution or plan to add support for unit testing. Thanks

IonicaBizau commented 8 years ago

Discussed here: https://github.com/thlorenz/proxyquire/issues/120

Glad to see the issue was fixed.