gpt4 book ai didi

javascript - 类型错误 : Cannot set property '_eventListeners' of undefined

转载 作者:行者123 更新时间:2023-11-29 16:01:28 25 4
gpt4 key购买 nike

使用 React + jest + Enzyme 运行测试时出现以下错误,请帮忙!

● 测试套件运行失败
类型错误:无法设置未定义的属性“_eventListeners”
在 Window.close (E:/TEST-UI/node_modules/jsdom/lib/jsdom/browser/Window.js:498:51)
在 Generator.throw ()

这个错误很奇怪!即使我尝试像下面这样设置 jsdom,但没有任何效果!

const jsdom = new JSDOM('<!doctype html><html><body></body></html>');

下面是代码片段。

import Enzyme from 'enzyme';

import Adapter from 'enzyme-adapter-react-16';
import 'jsdom-global/register';
import "isomorphic-fetch";


Enzyme.configure({adapter: new Adapter()});

export * from 'enzyme';

测试:

import React from "react";
import Notification from "./Notification";
import { shallow, mount, render } from 'enzyme';
import "src/config/jsdom.setup";

describe('<Notification />', () => {
test("always renders notificationCount", () => {
let wrapper = shallow(<Notification/>);
const divs = wrapper.find("div");
expect(divs.length).toBeGreaterThan(0);

});
});

>

require('babel-register')();

const jsdom = require('jsdom').jsdom;
const 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'};

package.json 片段:

 "devDependencies": {
"@babel/core": "^7.1.0",
"@babel/plugin-transform-runtime": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.4.2",
"babel-loader": "^8.0.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-polyfill": "^6.26.0",
"css-loader": "^0.28.11",
"enzyme": "^3.6.0",
"enzyme-adapter-react-16": "^1.5.0",
"enzyme-to-json": "^3.3.0",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"jest": "^23.5.0",
"jsdom": "^11.12.0",
"jsdom-global": "3.0.2",
"mini-css-extract-plugin": "^0.4.1",
"raw-loader": "^0.5.1",
"react-hot-loader": "^4.1.1",
"regenerator-runtime": "^0.12.1",
"style-loader": "^0.21.0",
"url-loader": "^1.1.1",
"webpack": "^4.19.1",
"webpack-cli": "^3.0.3",
"webpack-dev-server": "^3.1.4"
},

最佳答案

对于 JSDOM,您是否应该指定要访问的 DOM 中的对象?你有 global.document = jsdom(''); 的地方可能会导致错误。您应该将它指向窗口还是文档?在这种情况下,节点模块是抛出错误的地方,这通常是由于未正确实现或由于某些不兼容造成的。在这种情况下,我的猜测是前者。虽然我不知道有问题的图书馆,但我对此类事情的经验告诉我,它可能没有得到正确实现。

我建议查看的另一件事是您的循环方法。执行 Object.keys().forEach() 看起来很困惑且效率低下,而您可以轻松地执行一个更简单的 for 循环——大致如下:

for (let property in document.defaultView) {
if (document.defaultView.hasOwnProperty(property)) {
exposedProperties.push(property);
global[property] = document.defaultView[property];
}
}

关于javascript - 类型错误 : Cannot set property '_eventListeners' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52709413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com