测试无法读取属性InnerHTML的null

问题描述 投票:0回答:1

尝试对以下代码运行单元测试:使用Jest和Enzyme进行反应Js

在将一个返回插入到mock函数后,我不断收到错误:Test无法读取null的属性InnerHTML

这是mock函数:const getCleanJSON = jest.fn(()=> {return'{}'})

如果没有返回,我会收到以下错误:无法读取null的属性'appendChild'。我可能在模拟函数中遗漏了一些东西。有线索吗?

const s = document.createElement('script');
  s.id = 'script'
  s.type = 'text/template';
  s.async = true;
  s.innerHTML = script;
  this.instance.appendChild(s);

  this.renderView(serverSideRender);

这是错误所指的第二种方法

renderview() {
 .....
  return (
    engine.parseAndRender( document.getElementById('script').innerHTML, cleanJSON)
     .then( (html) => {
     if(document.getElementById('result')){ 
     document.getElementById('result').innerHTML = html == 'null'? '': html
  }
 }
 )
   .catch(e => document.getElementById('result').innerHTML = pretty(this.escapeHTML(e.stack)))
 )}
reactjs jestjs enzyme appendchild
1个回答
0
投票

也许直接从mock函数返回对象?

const getCleanJSON = is.fn(()=> {})

© www.soinside.com 2019 - 2024. All rights reserved.