Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.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
Javascript React测试库-使用render时TypeError trim不是函数_Javascript_React Testing Library - Fatal编程技术网

Javascript React测试库-使用render时TypeError trim不是函数

Javascript React测试库-使用render时TypeError trim不是函数,javascript,react-testing-library,Javascript,React Testing Library,我正在使用Jest+React测试库创建一个简单的测试。然而,当我试图渲染我的代码时,它会在de代码中崩溃。这很奇怪,因为应用程序确实运行平稳。道具似乎没有定义或是空的 测试文件: test('renders learn react link',()=>{ 常量{container}=render( ); } 导出默认应用程序; 应用程序的子级: import*as React from“React”; 导出类子类扩展React.Component{ const testString=this

我正在使用Jest+React测试库创建一个简单的测试。然而,当我试图渲染我的代码时,它会在de代码中崩溃。这很奇怪,因为应用程序确实运行平稳。道具似乎没有定义或是空的

测试文件:

test('renders learn react link',()=>{
常量{container}=render(
);
}
导出默认应用程序;
应用程序的子级:

import*as React from“React”;
导出类子类扩展React.Component{
const testString=this.props.testVariable.trim();
render(){
返回(
只是一个简单的div
)
}
}
接口程序{
testString:string
}

问题在于,您没有将
testVariable
的属性传递给
,因此如果您将该属性标记为可选,则应在继续任何操作之前检查该属性。此外,通过在上述类中定义变量,您的代码看起来是错误的

无论如何,您可以定义默认属性或空检查属性:

导出类子类扩展React.Component{
//设置默认道具
静态defaultProps={
testVariable:“
}
render(){
const testString=this.props.testVariable.trim();
返回(
只是一个简单的div
)
}
}
或空检查:

const testString=this.props.testVariable?.trim();

我还建议使用比使用类容易得多的功能组件,这样您也可以利用使用钩子。但是如果我不想进行空检查,我应该怎么做?我应该在测试中通过参数吗?我会记住功能组件:)。因为现在我们确定运行测试时testVariable为空。但这不是最好的练习,对吗?你必须决定哪些道具是必需的。如果需要,您必须提供。否则,您只需提供默认值或null checkingHmm好的,我如何才能在测试中将这些道具发送给孩子?因为我在渲染。是这样的吗