Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 不变冲突:元素类型无效:使用酶装入测试时应为字符串(对于内置组件)_Reactjs_Testing_Jestjs_Enzyme - Fatal编程技术网

Reactjs 不变冲突:元素类型无效:使用酶装入测试时应为字符串(对于内置组件)

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

我正在测试一个包装器组件,该组件使用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 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时,一切都正常。或者,如果我使用浅层而不是酶的装载,这也很好。有同样的问题:(有同样的问题,酶可以正常工作,但是装载会出错