我的js文件需要测试导入图标字体.js lib在此组件中使用icon。但是当我运行 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
来模拟 ,但没有用。
我是反应单元测试的新手,请帮助我。
我自己解决这个问题。 我使用require()
而不是import
,并在需要之前将<script></script>
分配给document.body.innerHTML
。 如下所示:
//old
import component from 'need-test-component';
//new
document.body.innerHTML = '<script></script>'
const component = require('need-test-component').default;