【发布时间】:2018-02-25 14:33:12
【问题描述】:
我正在使用 jest 来测试带有来自 react-router v4 的 <Link> 的组件。
我收到警告,<Link /> 需要来自 react-router <Router /> 组件的上下文。
如何在我的测试中模拟或提供路由器上下文? (基本上我该如何解决这个警告?)
Link.test.js
import React from 'react';
import renderer from 'react-test-renderer';
import { Link } from 'react-router-dom';
test('Link matches snapshot', () => {
const component = renderer.create(
<Link to="#" />
);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
测试运行时的警告:
Warning: Failed context type: The context `router` is marked
as required in `Link`, but its value is `undefined`.
【问题讨论】:
标签: react-router jestjs react-router-v4