【发布时间】:2018-07-22 04:59:17
【问题描述】:
我正在尝试使用反应路由器 v4 和开玩笑酶进行简单测试。
describe('<App />', () => {
it('renders a static text', () => {
const wrapper = shallow(
<MemoryRouter initialEntries={['/']} initialIndex={0}>
<App/>
</MemoryRouter>
console.log(wrapper.html());
);
});
});
当我尝试 console.log(wrapper.html()) 时,我得到:
Invariant Violation: [React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry.
我正在使用 react-boiler-plate 项目。我想要做的就是测试当我给它一个'/'的路径时,主页会呈现一些文本。
任何帮助表示赞赏!谢谢!
编辑:
这是测试:
it('test', () => {
const wrapper = shallow(
<MemoryRouter initialEntries={['/']} initialIndex={0}>
<Route path="/" render={() => <App/>}/>
</MemoryRouter>,
);
console.log(wrapper.find(App).html());
});
这是 App 类:
export default function App() {
return (
<AppWrapper>
<Helmet
titleTemplate="%s - React.js Boilerplate"
defaultTitle="React.js Boilerplate"
>
<meta name="description" content="A React.js Boilerplate application"/>
</Helmet>
<Header/>
<Switch>
<Route exact path="/" component={HomePage}/>
<Route path="/features" component={FeaturePage}/>
<Route
path="/catalog"
render={() => <div>hi</div>}
/>
<Route path="" component={NotFoundPage}/>
</Switch>
<Footer/>
</AppWrapper>
);
}
这是运行测试时的错误信息:
**Error: Method “html” is only meant to be run on a single node. 0 found instead.**
我希望这个页面会匹配,并且我能够 console.log 它的输出:
<Route exact path="/" component={HomePage}/>
【问题讨论】:
标签: reactjs testing react-router router jestjs