Search code examples
testingnockredux-observable

Nock, localhost, port, and redux-observable


I have a mock api setup with node/express

http://localhost:8080/api/lyric/

I'm using redux-observable to fetch the json:

const fetchLyricEpic = action$ =>
action$.ofType(FETCH_LYRIC)
  .mergeMap(action =>
    ajax.getJSON('/api/lyric')
      .map(response => fetchLyricSuccess(response))
      .catch(error => Observable.of(fetchLyricFail(error)))
  )

The problem is, when I test, I am getting an api error instead of 200 using nock:

beforeEach(() => {
  nock.disableNetConnect()
  nock.enableNetConnect('127.0.0.1:3000/')
})

afterEach(() => {
  nock.cleanAll()
  nock.enableNetConnect()
  epicMiddleware.replaceEpic(rootEpic)
})

test('it creates FETCH_SUCCESS when fetching the lyric has been done', () => {
  const payload = {
    status: 200,
    response: {
      'lyric': "Lyric"
    }
  }
  nock('127.0.0.1:3000/')
    .get('/api/lyric')
    .reply(200, payload, {'Content-Type': 'application/json'})

I'm trying to follow what I read in the nock docs and also in the issues list, however, my nock api call keeps failing.

Expected value to equal:
      [{"type": "FETCH_LYRIC"}, {"payload": {"lyric": "Lyric"}, "type": "FETCH_LYRIC_SUCCESS"}]
    Received:
      [{"type": "FETCH_LYRIC"}, {"payload": {}, "type": "FETCH_LYRIC_ERROR"}]

I should be receiving FETCH_LYRIC_SUCCESS instead I see:

[ { type: 'FETCH_LYRIC' },
        { type: 'FETCH_LYRIC_ERROR',
          payload:
           SyntaxError
               at XMLHttpRequest.open (<user>/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:486:15)
               at XMLHttpRequest.tryCatcher (<user>/node_modules/rxjs/util/tryCatch.js:6:31)
               at AjaxSubscriber.Object.<anonymous>.AjaxSubscriber.send (<user>/node_modules/rxjs/observable/dom/AjaxObservable.js:208:56)
               at new AjaxSubscriber (<user>/node_modules/rxjs/observable/dom/AjaxObservable.js:180:14)
               at AjaxObservable.Object.<anonymous>.AjaxObservable._subscribe (<user>/node_modules/rxjs/observable/dom/AjaxObservable.js:115:16)
               at AjaxObservable.Object.<anonymous>.Observable._trySubscribe (<user>/node_modules/rxjs/Observable.js:57:25)
               at AjaxObservable.Object.<anonymous>.Observable.subscribe (<user>/node_modules/rxjs/Observable.js:45:27)
               at MapOperator.Object.<anonymous>.MapOperator.call (<user>/node_modules/rxjs/operator/map.js:54:23)
               at Observable.Object.<anonymous>.Observable.subscribe (<user>/node_modules/rxjs/Observable.js:42:22)
               at MapOperator.Object.<anonymous>.MapOperator.call (<user>/node_modules/rxjs/operator/map.js:54:23) } ]

Solution

  • From the error message, it sounds identical to the issues described here: https://github.com/tmpvar/jsdom/issues/1706

    I am working on a project that uses mocha with jsdom and enzyme to run our integration tests within a virtual dom in Node. Right now I am using node 6.9.2, and everything works fine. I tried using 7.4.0, however running the tests I got the error

    (nearly identical stack trace)

    If indeed it is the same, some have reported this was an issue with Node 7.4.0 and that upgrading to 7.5.0 or higher solved the problem.