【问题标题】:Test cannot read property InnerHTML of null测试无法读取 null 的属性 InnerHTML
【发布时间】:2019-08-09 06:36:18
【问题描述】:

尝试对以下代码运行单元测试:Using Jest and Enzyme for React Js

在模拟函数中插入返回后,我不断收到错误消息: 测试无法读取 null 的属性 InnerHTML

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

如果没有 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】:

    也许直接从模拟函数中返回对象?

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

    【讨论】:

    • 不,它不能解决任何问题 - 在 Json 中得到一个意外令牌的不同错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 2021-01-13
    • 1970-01-01
    • 2022-12-20
    • 2020-10-10
    • 2018-06-09
    • 1970-01-01
    相关资源
    最近更新 更多