2017-05-19 13 views
2

を使用する場合は、以下の構成要素を考えると、「ヌルのプロパティ_locationを読み取ることができません」:私は時に次のエラーを取得する冗談テストケースにはアポロに反応

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

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

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

export default graphql(Query)(MatchListRouteHandler); 

とテストケースを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

ちょっとメモしておきます。私は今日、Apollo + Jestでもこのエラーに遭遇しましたが、もっとエレガントな方法で修正されましたが、私はafterEachでテストされているコンポーネントをアンマウントしました。

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

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

これは、Jestテストプロセスが速すぎるために発生すると思われます。 Apolloは、のデータを依頼し続けます。のアプリケーションがマウントされましたが、の後に応答が発生しません。のテストが終了したため、Jestランナー全体が終了する。

これはすなわち、問題のテストが終了する前に人為的に遅延を増やすことで改善することができます。これは完全にエラーを修正しないこと

​​

は注意(実際には、上記のテストは必ず合格する)が、テストランナー全体が完全に爆発しないことを意味します。

正解は(おそらく)です。テストではServer Side Renderingが使用されます。

関連する問題