ES6导入导出模块
ES6 import and export modules
在另一个文件中,我是这样导入的,但是当我在测试中使用时出现错误 React.createElement: type is invalid -- expected a string (for built-in components) or a class/function(对于复合组件)但得到:未定义。您可能忘记从定义它的文件中导出您的组件,或者您可能混淆了默认导入和命名导入。可以是什么?
import { RateComponent, mapStateToProps } from '../components/Rate';
it ('renders correctly if has data', () => {
const tree = shallow( <RateComponent dispatch={jest.fn()}/>);
expect(tree).toMatchSnapshot();
});
这是我导出的方式。
export
{
RateComponent,
mapStateToProps
}
export default connect(mapStateToProps)(RateComponent);
尝试导入默认导出并查看是否有效
import RateComponent from '../components/Rate';
此外,您不需要导出 mapStateToProps
。
正如 Clarity 已经指出的那样,只需从文件中导入 RateComponent。并通过
RateComponent 的一些道具用于测试浅渲染组件和匹配
快照
it ('renders correctly if has data', () => {
const props = {
// ...Define your props here with some values
};
const tree = shallow( <RateComponent {...props}/>);
expect(tree).toMatchSnapshot();
});
在另一个文件中,我是这样导入的,但是当我在测试中使用时出现错误 React.createElement: type is invalid -- expected a string (for built-in components) or a class/function(对于复合组件)但得到:未定义。您可能忘记从定义它的文件中导出您的组件,或者您可能混淆了默认导入和命名导入。可以是什么?
import { RateComponent, mapStateToProps } from '../components/Rate';
it ('renders correctly if has data', () => {
const tree = shallow( <RateComponent dispatch={jest.fn()}/>);
expect(tree).toMatchSnapshot();
});
这是我导出的方式。
export
{
RateComponent,
mapStateToProps
}
export default connect(mapStateToProps)(RateComponent);
尝试导入默认导出并查看是否有效
import RateComponent from '../components/Rate';
此外,您不需要导出 mapStateToProps
。
正如 Clarity 已经指出的那样,只需从文件中导入 RateComponent。并通过 RateComponent 的一些道具用于测试浅渲染组件和匹配 快照
it ('renders correctly if has data', () => {
const props = {
// ...Define your props here with some values
};
const tree = shallow( <RateComponent {...props}/>);
expect(tree).toMatchSnapshot();
});