Nock、本地主机、端口和 redux-observable

Nock, localhost, port, and redux-observable

我有一个模拟 api 设置 node/express

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

我正在使用 redux-observable 获取 json:

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

问题是,当我测试时,我得到一个 api 错误而不是 200 使用 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'})

我正在尝试按照我在箭尾文档和问题列表中阅读的内容进行操作,但是,我的箭尾 api 调用一直失败。

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

我应该收到 FETCH_LYRIC_SUCCESS 而我却看到了:

[ { 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) } ]

从错误消息来看,这听起来与此处描述的问题相同: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)

如果确实相同,有人报告说这是 Node 7.4.0 的问题,升级到 7.5.0 或更高版本可以解决问题。