【发布时间】:2017-10-01 08:12:34
【问题描述】:
我一直在努力嘲笑 React-Intl library 和 Jest,因为我在运行测试时遇到了这个错误:
Invariant Violation: [React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry.
这个库的documentation 说我们必须在根项目中创建一个名为__Mocks__ 的文件夹,然后添加这个文件:
// ./__mocks__/react-intl.js
import React from 'react';
const Intl = require.requireActual('react-intl');
// Here goes intl context injected into component, feel free to extend
const intl = {
formatMessage: ({defaultMessage}) => defaultMessage
};
Intl.injectIntl = (Node) => (props) => <Node {...props} intl={intl}/>;
module.exports = Intl;
但是什么也没发生。
【问题讨论】:
标签: javascript unit-testing reactjs jestjs react-intl