2017-05-19 26 views
2

鑑於以下組件:用一個玩笑測試用例陣營阿波羅時「無法讀取空的財產_location」

export function App() { 
    return withApollo(<BrowserRouter> 
    <MatchListRouteHandler /> 
    </BrowserRouter>); 
} 

// MatchListRouteHandler 
export const Query = addTypenameToDocument(gql` 
    query GetMatches { 
    matches { 
     id 
    } 
    } 
`); 

export default graphql(Query)(MatchListRouteHandler); 

而且測試用例:

it('renders without crashing',() => { 
    const div = document.createElement('div'); 
    ReactDOM.render(<App />, div); 
}); 

我收到以下錯誤,當Jest試圖運行測試用例:

/home/dan/match-history-analyser/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] (/home/dan/Projects/match-history-analyser/node_modules/jsdom/lib/jsdom/browser/Window.js:148:79) 
    at Timeout.callback [as _onTimeout] (/home/dan/Projects/match-history-analyser/node_modules/jsdom/lib/jsdom/browser/Window.js:525:40) 
    at ontimeout (timers.js:386:14) 
    at tryOnTimeout (timers.js:250:5) 
    at Timer.listOnTimeout (timers.js:214:5) 

回答

1

這個剛一說明。我今天碰到這個錯誤來了,也與阿波羅+玩笑,但我在什麼可能是一個更優雅的方式,不知道固定,但我卸除在afterEach被測試的組件。

beforeEach(() => { 
    wrapper = mount(<Root location={ '/route/' } context={context} />); 
}) 

afterEach(() => { 
    wrapper.unmount(); 
}); 
2

這似乎是因爲Jest測試過程退出得太快;阿波羅試圖繼續要求您提供後的應用程序被安裝在數據,但響應不會發生,直到後的測試已經結束,從而導致整個玩笑亞軍退出這個神祕的錯誤信息。

這可以通過有關的測試之前,人爲地增加了立即補救結束,即:

​​

注意,這並不完全修復錯誤(實際上,上述測試將永遠傳遞),但這確實意味着你的整個測試運動員不會完全爆炸。

正確答案或許涉及在測試中使用Server Side Rendering