0

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

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'})

我正在尝试遵循我在 nock 文档和问题列表中阅读的内容,但是,我的 nock 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) } ]
4

1 回答 1

1

从错误消息来看,这听起来与此处描述的问题相同:https ://github.com/tmpvar/jsdom/issues/1706

我正在开发一个项目,该项目使用 mocha 和 jsdom 和酶在 Node.js 的虚拟 dom 中运行我们的集成测试。现在我正在使用节点 6.9.2,一切正常。我尝试使用 7.4.0,但是运行测试我得到了错误

(几乎相同的堆栈跟踪)

如果确实是一样,有人报告说这是 Node 7.4.0 的问题,升级到 7.5.0 或更高版本解决了这个问题。

于 2017-04-24T01:32:56.513 回答