Javascript 如何嘲笑一个&书信电报;脚本>&引用;即时单元测试
我的js文件需要测试导入iconfont.js库以在此组件中使用图标。但当我运行npm测试时,它显示: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
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,但不一定如此。改为Mockdocument.getElementsByTagName
。最好不要这样做。JSDOM的行为可能类似于真实的DOM,但不一定如此。改为Mockdocument.getElementsByTagName
。