Reactjs 如何解决无法设置属性'_eventListeners';jest/jsdom的未定义错误?
我正在用玩笑代替因果报应,当我创建了所有需要的文件和设置时,你们可以在下面看到。当我执行npm jest时,我得到一个错误:Reactjs 如何解决无法设置属性'_eventListeners';jest/jsdom的未定义错误?,reactjs,typescript,webpack,jestjs,jsdom,Reactjs,Typescript,Webpack,Jestjs,Jsdom,我正在用玩笑代替因果报应,当我创建了所有需要的文件和设置时,你们可以在下面看到。当我执行npm jest时,我得到一个错误: Test suite failed to run TypeError: Cannot set property '_eventListeners' of undefined at Window.close (node_modules/jsdom/lib/jsdom/browser/Window.js:475:51) 我找到了一个解决方案,可以编辑没有意义的node_mo
Test suite failed to run
TypeError: Cannot set property '_eventListeners' of undefined
at Window.close (node_modules/jsdom/lib/jsdom/browser/Window.js:475:51)
我找到了一个解决方案,可以编辑没有意义的node_modules/*文件。
当我尝试将TestenEnvironment切换到“node”时,它看起来不错,但我认为这不是一个好的解决方案,因为它不是node service项目
/jest.config.js
module.exports = {
coverageReporters: [
"html",
"text",
"cobertura"
],
globals: {
"ts-jest": {
tsConfig: "tsconfig.test.json",
babelConfig: true
}
},
moduleFileExtensions: [
"ts",
"tsx",
"js",
"jsx",
"json",
"node",
"d.ts",
"scss"
],
moduleNameMapper: {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/test/mocks/fileMock.js",
"\\.(css|sass|scss)$": "<rootDir>/test/mocks/styleMock.js",
"^worker-loader!": "<rootDir>/test/mocks/workerLoaderMock.js",
"^resize-observer-polyfill$": "<rootDir>/test/mocks/resizeObserverPolyfillMock.js",
"applicationinsights-js": "<rootDir>/test/mocks/appInsightsMock.js"
},
reporters: [
"default",
"jest-junit"
],
setupFiles: [
"<rootDir>/test/mocks/setupJest.js"
],
setupFilesAfterEnv: [
"<rootDir>/test/mocks/matchMedia.js"
],
transform: {
"^.+\\.(js|jsx|ts|tsx|d.ts)$": "ts-jest"
},
testMatch: [
"<rootDir>/test/**/*-test.tsx"
],
timers: "fake"
};
my-test.js
import { Tooltip } from "../../src/components/shared";
import { expect, React, shallow } from "../test-utils";
describe("Tooltip with empty properties", () => {
const component = shallow(<Tooltip tooltipText="" />);
it("should have no text", () => {
expect(component.find("span.tooltip-text").text()).to.have.length(0);
});
it("should have 'tooltip-container' class", () => {
expect(component.hasClass("tooltip-container")).to.be.true;
});
it("should have child with 'tooltip-text' class", () => {
expect(component.childAt(0).hasClass("tooltip-text")).to.be.true;
});
});
describe("Tooltip with set properties", () => {
const expectedClass = "test-class";
const expectedText = "Test tooltip text";
const component = shallow(<Tooltip tooltipText={expectedText} className={expectedClass} />);
it("should have the expected text", () => {
expect(component.find("span.tooltip-text").text()).to.be.equal(expectedText);
});
it("should have the expected classes", () => {
expect(component.hasClass("tooltip-container")).to.be.true;
expect(component.hasClass(expectedClass)).to.be.true;
});
});
从“./../src/components/shared”导入{Tooltip};
从“./test utils”导入{expect,React,shall};
描述(“具有空属性的工具提示”,()=>{
常量分量=浅();
它(“应该没有文本”,()=>{
expect(component.find(“span.tooltip text”).text()到.have.length(0);
});
它(“应该有‘工具提示容器’类”,()=>{
expect(component.hasClass(“工具提示容器”).to.be.true;
});
它(“应该有具有“工具提示文本”类的子项”,()=>{
expect(component.childAt(0.hasClass(“工具提示文本”)).to.be.true;
});
});
描述(“具有设置属性的工具提示”,()=>{
const expectedClass=“测试类”;
const expectedText=“测试工具提示文本”;
常量分量=浅();
它(“应该有预期的文本”,()=>{
expect(component.find(“span.tooltip text”).text()).to.be.equal(expectedText);
});
它(“应该有预期的类”,()=>{
expect(component.hasClass(“工具提示容器”).to.be.true;
expect(component.hasClass(expectedClass)).to.be.true;
});
});
你能把失败测试的内容和你的setupJes.js中的内容放进去吗?我有17个测试,每个测试都失败了。setupJest.js:global.Intl=require(“Intl”);global.fetch=require('jest-fetch-mock')global.AppSettings={AUTHORITY\u URL:“https://auth.blah.com,插装键:“应用程序设置键”,实时URL库:“https/:REALTIME.blah.com”}代码>您的测试失败,因为您有未定义的内容。我们不能不看它就知道它是什么。我在评论上面加了测试。
import { Tooltip } from "../../src/components/shared";
import { expect, React, shallow } from "../test-utils";
describe("Tooltip with empty properties", () => {
const component = shallow(<Tooltip tooltipText="" />);
it("should have no text", () => {
expect(component.find("span.tooltip-text").text()).to.have.length(0);
});
it("should have 'tooltip-container' class", () => {
expect(component.hasClass("tooltip-container")).to.be.true;
});
it("should have child with 'tooltip-text' class", () => {
expect(component.childAt(0).hasClass("tooltip-text")).to.be.true;
});
});
describe("Tooltip with set properties", () => {
const expectedClass = "test-class";
const expectedText = "Test tooltip text";
const component = shallow(<Tooltip tooltipText={expectedText} className={expectedClass} />);
it("should have the expected text", () => {
expect(component.find("span.tooltip-text").text()).to.be.equal(expectedText);
});
it("should have the expected classes", () => {
expect(component.hasClass("tooltip-container")).to.be.true;
expect(component.hasClass(expectedClass)).to.be.true;
});
});