jsdom:看不懂财产' _location'为null

时间:2017-04-03 16:52:14

标签: reactjs unit-testing jestjs jsdom

当我通过开玩笑测试史诗时,我的测试失败了。

Test Failed
/.../node_modules/jsdom/lib/jsdom/browser/Window.js:148

return idlUtils.wrapperForImpl(idlUtils.implForWrapper(window._document)._location);

TypeError: Cannot read property '_location' of null
    at Window.get location [as location] (/.../node_modules/jsdom/lib/jsdom/browser/Window.js:148:79)
    at Timeout.callback [as _onTimeout] (/.../node_modules/jsdom/lib/jsdom/browser/Window.js:525:40)
    at ontimeout (timers.js:380:14)
    at tryOnTimeout (timers.js:244:5)
    at Timer.listOnTimeout (timers.js:214:5)

这是我的一些测试。我在fetchCategoriesEpic中使用了fetch()和promise。

it('test epic', () => {
    ...
    const action$ = ActionsObservable.of({ type: FETCH_CATEGORIES })

    fetchCategoriesEpic(action$)
      .toArray() // collects everything in an array until our epic completes
      .subscribe((actions) => {
        expect(actions).toEqual([
          { type: FETCH_CATEGORIES },
          { type: FETCH_CATEGORIES_SUCCESS, payload: List(payload.data).map((category) => new Category(category)) }
        ])
      })
    ...
}

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

看来这个问题只与jsdom有关,而不是redux-observable,  所以你可能想要改变你的标题,描述,标签。您可能希望在测试中包含jsdom的任何配置/用法以及您正在使用的节点版本。

Others have reported过去类似的问题,但我无法通过快速Google搜索找到明确的解决方案。我的直觉会告诉我你的配置稍微偏离它并没有意识到它应该为你创建一个虚假的位置对象。

答案 1 :(得分:0)

在未定义document.documentElement的环境中尝试使用document时遇到此问题。

我在应用程序代码的那一部分添加了一个条件,之后我的测试运行正常。

确保您的应用程序代码而不是测试代码试图仅访问测试环境中存在的元素。