Reactjs 如何在Jest Mock组件中返回字符串';没有流抱怨的渲染方法?
我在一个Jest测试中模拟React组件,flow正在抱怨:Reactjs 如何在Jest Mock组件中返回字符串';没有流抱怨的渲染方法?,reactjs,jestjs,flowtype,Reactjs,Jestjs,Flowtype,我在一个Jest测试中模拟React组件,flow正在抱怨: [flow] string (This type is incompatible with the expected return type of React$Element) 我该如何考虑这一点?这是我的密码: jest.mock('../../../../ui-components/lib/Forms/TextField', () => { const React = require('react'); const
[flow] string (This type is incompatible with the expected return type of React$Element)
我该如何考虑这一点?这是我的密码:
jest.mock('../../../../ui-components/lib/Forms/TextField', () => {
const React = require('react');
const TextField = class extends React.Component {
render() {
return 'TextField'; // Flow complains here
}
};
return TextField;
});
编辑:
对于那些谷歌用户,我就是这么做的:
jest.mock('../../../../ui-components/lib/Forms/TextField', () => {
const React = require('react');
const TextField = class extends React.Component {
render() {
return <div />;
}
};
return TextField;
});
jest.mock('../../../../ui组件/lib/Forms/TextField',()=>{
const React=require('React');
const TextField=类扩展了React.Component{
render(){
返回;
}
};
返回文本字段;
});
我是在一个错误的假设下操作的,即酶的浅包装器不会定位组件,除非我在这里返回字符串。老实说,我想不起我为什么会这样想,因为现在看来很荒谬
我在下面标出了我原来问题的答案,因为如果我没有采取更好的方法,这是正确的。//$FlowFixMe的使用我不知道您为什么要这样做,但如果您对此有把握,可以在出现错误的行上方添加一条
/$FlowFixMe
注释
/$FlowFixMe
希望这有帮助。为什么需要它来返回原始字符串?为什么不
null
或div
或其他什么?你是对的,在这种情况下我应该使用div。我认为我遇到的问题是,我认为我的测试无法在浅层渲染包装器中定位组件,除非我使用该字符串。