1
我測試呈現具有以下contextTypes子組件一個組件的組件:玩笑 - 測試使用反應路由器
Component.contextTypes = {router: PropTypes.object.isRequired}
我完全新的笑話,但是從摩卡/酶來的時候我從來沒有遇到過這個問題。
我的第一個測試是這樣的,而且我真的只是瞎搞吧,看看它是如何工作的:
it('should exist',() => {
wrapper = renderer.create(<Companies/>);
let tree = wrapper.toJSON();
expect(tree).toMatchScreenshot();
});
當我運行測試,我得到以下錯誤:
Failed context type: The context `router` is marked as required in `ChildComponent`, but its value is `undefined`.
有沒有解決這個問題的方法,或者僅僅是我錯過的文檔中的一些東西?提前致謝!
解決方案:對於運行到同一問題的人,我曾經在一個beforeEach()以下:
MyComponent.contextTypes = {
router: function() {
return {
transitionTo: jest.genMockFunction()
};
}
};
它使用哪個版本的react-router? – paqash
@paqash版本2.4.1 – Jake