Relative Url Testing — React, Redux with Nock

Colin McDonald
1 min readMay 23, 2016

--

I’ve been working on a project and trying to better my Test Driven Development but ran into a problem with relative url testing using the Nock library. Redux’s Dan Abramov recommends (http://redux.js.org/docs/recipes/WritingTests.html) to use the Nock library when testing asynchronous action creators. However, if you need your production code to use relative urls Nock does not support this.

Even after trying a few other libraries, including fetch-mock, I could not successfully test async action creators when using relative urls. As a result, I had to come up with a creative solution. The basic idea is that your async action creators will take an extra argument, called ‘testing’. We pass in the absolute url domain we’ll be testing, and that async action creator will add the relative url to that domain like so:

--

--