Reactjs 不变冲突:元素类型无效:使用酶装入测试时应为字符串(对于内置组件)
我正在测试一个包装器组件,该组件使用children道具呈现其子组件。问题是,在测试文件中,当我设置包装器时,它工作正常。但是,当我克隆组件中的子道具时,会出现以下错误:Reactjs 不变冲突:元素类型无效:使用酶装入测试时应为字符串(对于内置组件),reactjs,testing,jestjs,enzyme,Reactjs,Testing,Jestjs,Enzyme,我正在测试一个包装器组件,该组件使用children道具呈现其子组件。问题是,在测试文件中,当我设置包装器时,它工作正常。但是,当我克隆组件中的子道具时,会出现以下错误: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You l
Invariant Violation: Element type is invalid: expected a string (for
built-in components) or a class/function (for composite components) but
got: undefined. You likely forgot to export your component from the
file it's defined in, or you might have mixed up default and named imports.
这只发生在使用mount而不是shallow时,我希望它能与mount一起工作
class Parent extends React.Component {
render() {
const { someProps, someOtherProps, anotherProp } = this.props;
return (
<ParentWrapper someInfo={someProps} >
<One someOtherProps={someOtherProps} />
<Two anotherProp={anotherProp} />
</ParentWrapper>
);
}
}
类父级扩展React.Component{
render(){
const{someProps,someOtherProps,anotherProp}=this.props;
返回(
);
}
}
父包装器:
class ParentWrapper extends React.Component {
render() {
const {children, somethin} = this.props;
const clonedChildren = React.cloneElement(children, R.merge(children.props, { somethin }));
return (
<div>
{clonedChildren}
</div>
);
}
}
类ParentWrapper扩展React.Component{
render(){
const{children,somethin}=this.props;
const clonedChildren=React.cloneElement(children,R.merge(children.props,{somethin}));
返回(
{克隆儿童}
);
}
}
测试文件:
const Parent = require('parent');
panel = mount(<Parent {...defaultProps} />);
const Parent=require('Parent');
面板=安装();
我也遇到过这个问题。关于SO的大多数其他类似答案都涉及正确的导入/导出,但当我使用shallow()
时,它工作正常,这意味着它不是导入/导出
最终我发现,
react
和react-dom
的版本稍微不匹配。(提醒,因为我需要它,请删除现有的node_模块
和package lock.json
,以确保您确实正在提取更新的模块)。这为我解决了这个问题。当您不小心遗漏了export
语句或export
错误的组件名称时,就会发生这种情况。我建议您查看您的export
语句。我已经检查了我的导出,并且我的方式看起来很好。奇怪的是,当我没有在ParentWrapper中克隆子对象,并使用children而不是clonedChildren时,一切都正常。或者,如果我使用浅层而不是酶的装载,这也很好。有同样的问题:(有同样的问题,酶可以正常工作,但是装载会出错