2017-04-03 116 views
0

当我通过笑话测试史诗时,我得到测试失败。jsdom:无法读取null的属性'_location'

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)) } 
     ]) 
     }) 
    ... 
} 

我该如何解决这个问题?

回答

0

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

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

0

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

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

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