我一直在努力用Jest模拟 React-Intl 库,因为我在运行测试时遇到了这个错误:
Invariant Violation: [React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry.
这个库的文档说我们必须在根项目中创建一个名为的文件夹__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;
但什么也没有发生。