Error: It looks like you called `mount()` without a global document being loaded
Error: It looks like you called `mount()` without a global document being loaded
我正在尝试安装一个组件以使用酶进行测试,但出现此错误。
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了解更多信息
npm install --save-dev --save-exact jsdom jsdom-global
然后:
import 'jsdom-global/register'; //at the top of file, even before importing React
更多信息here。
备选方案:如果您使用的是 jest with enzyme-
在你的package.json
中指定jest的测试环境为jsdom
如下:
"jest": {
"testEnvironment": "jsdom"
}
我遇到了同样的问题,它对我来说效果很好。
只需将以下内容放在测试文件的顶部
/**
* @jest-environment jsdom
*/
我正在尝试安装一个组件以使用酶进行测试,但出现此错误。
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了解更多信息
npm install --save-dev --save-exact jsdom jsdom-global
然后:
import 'jsdom-global/register'; //at the top of file, even before importing React
更多信息here。
备选方案:如果您使用的是 jest with enzyme-
在你的package.json
中指定jest的测试环境为jsdom
如下:
"jest": {
"testEnvironment": "jsdom"
}
我遇到了同样的问题,它对我来说效果很好。
只需将以下内容放在测试文件的顶部
/**
* @jest-environment jsdom
*/