我使用jest
來測試來自react-router v4的<Link>
組件。使用Jest測試來自react-router v4的鏈接
我收到一條警告,要求<Link />
需要來自反應路由器<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`.