加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

reactjs – 错误:看起来你没有加载全局文档就调用了`mount()`

发布时间:2020-12-14 02:07:32 所属栏目:Linux 来源:网络整理
导读:我正在尝试安装一个用酶测试的组件,并得到这个错误. 解决方法 Mocha不会在浏览器环境中运行您的测试,因此没有DOM.要解决此问题,只需使用jsdom npm模块来创建DOM. 从Enzyme docs开始: Since enzyme’s mount API requires a DOM,JSDOM is required in order
我正在尝试安装一个用酶测试的组件,并得到这个错误.

解决方法

Mocha不会在浏览器环境中运行您的测试,因此没有DOM.要解决此问题,只需使用jsdom npm模块来创建DOM.

从Enzyme docs开始:

Since enzyme’s mount API requires a DOM,JSDOM is required in order to
use mount if you are not already in a browser environment (ie,a Node
environment).

JSDOM is a JavaScript based headless browser that can be used to
create a realistic testing environment.

For the best experience with enzyme,it is recommended that you load a
document into the global scope before requiring React for the first
time. It is very important that the below script gets run before
React’s code is run.

As a result,a standalone script like the one below is generally a good approach:

/* setup.js */

var jsdom = require('jsdom').jsdom;

var exposedProperties = ['window','navigator','document'];

global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
  if (typeof global[property] === 'undefined') {
    exposedProperties.push(property);
    global[property] = document.defaultView[property];
  }
});

global.navigator = {
  userAgent: 'node.js'
};

阅读Enzyme documentation – JSDOM以获取更多信息

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读