Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.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 如何嘲笑一个&书信电报;脚本>&引用;即时单元测试_Javascript_Reactjs_Unit Testing_Jestjs - Fatal编程技术网

Javascript 如何嘲笑一个&书信电报;脚本>&引用;即时单元测试

Javascript 如何嘲笑一个&书信电报;脚本>&引用;即时单元测试,javascript,reactjs,unit-testing,jestjs,Javascript,Reactjs,Unit Testing,Jestjs,我的js文件需要测试导入iconfont.js库以在此组件中使用图标。但当我运行npm测试时,它显示: Test suite failed to run TypeError: Cannot read property 'getAttribute' of undefined 当我看到iconfont.js时,它有以下关于此错误的代码: h=(a=document.getElementsByTagName("script"))[a.length-1].getAttribute("data-inj

我的js文件需要测试导入iconfont.js库以在此组件中使用图标。但当我运行npm测试时,它显示:

Test suite failed to run

TypeError: Cannot read property 'getAttribute' of undefined
当我看到iconfont.js时,它有以下关于此错误的代码:

h=(a=document.getElementsByTagName("script"))[a.length-1].getAttribute("data-injectcss");
我尝试在测试文件中使用
document.body.innerHTML
来模拟,但没有用


我是react单元测试的新手,请帮助我。

我自己解决这个问题。 我使用
require()
而不是
import
,并在require之前将
分配给
document.body.innerHTML
。 如下图所示:

//old
import component from 'need-test-component';
//new
document.body.innerHTML = '<script></script>'
const component = require('need-test-component').default;
//旧的
从“需要测试组件”导入组件;
//新的
document.body.innerHTML=“”
const component=require('need-test-component')。默认值;

我自己解决这个问题。 我使用
require()
而不是
import
,并在require之前将
分配给
document.body.innerHTML
。 如下图所示:

//old
import component from 'need-test-component';
//new
document.body.innerHTML = '<script></script>'
const component = require('need-test-component').default;
//旧的
从“需要测试组件”导入组件;
//新的
document.body.innerHTML=“”
const component=require('need-test-component')。默认值;

您最好不要这样做。JSDOM的行为可能类似于真实的DOM,但不一定如此。改为Mock
document.getElementsByTagName
。最好不要这样做。JSDOM的行为可能类似于真实的DOM,但不一定如此。改为Mock
document.getElementsByTagName