我正在测试一个组件,该组件呈现具有以下 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()
};
}
};