IonicaBizau / node-ipinfo

:globe_with_meridians: An http://ipinfo.io NodeJS wrapper.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unit testing with ipInfo

souly1 opened this issue · comments

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

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. 💭

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

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.

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

Discussed here: thlorenz/proxyquire#120

Glad to see the issue was fixed.