gpt4 book ai didi

reactjs - 使用 Chai\Mocha 设置进行单元测试时,如何让 JSDOM 与 typescript 一起使用?

转载 作者:行者123 更新时间:2023-12-04 11:30:41 25 4
gpt4 key购买 nike

我一直在尝试让 JSDOM 在我的 typescript 单元测试中工作,但经过多次尝试,我不断收到以下错误(或以下错误的变体)

TypeError: _jsdom.JSDOM is not a constructor
代码片段如下
import React from 'react';

import { act } from 'react-dom/test-utils';
import { expect } from 'chai';
import { mock, instance, verify, anyOfClass } from 'ts-mockito';
import { JSDOM } from 'jsdom'
import { TestApp } from '../ts/ExampleForTestingReact';

let rootContainer: HTMLDivElement;
const { window } = new JSDOM('<!doctype html><html><body></body></html>');
我的包json如下
{
"name": "app",
"version": "1.0.0",
"private": true,
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/node": "^7.8.7",
"@babel/preset-env": "^7.9.0",
"@babel/preset-react": "^7.10.1",
"@babel/preset-typescript": "^7.9.0",
"@babel/register": "^7.9.0",
"@types/bootstrap": "4.3.2",
"@types/chai": "^4.2.0",
"@types/mocha": "^7.0.2",
"@types/react": "^16.9.31",
"@types/react-dom": "^16.9.6",
"@types/jsdom": "^16.2.3",
"babel-loader": "^8.1.0",
"chai": "^4.2.0",
"clean-webpack-plugin": "^3.0.0",
"css-loader": "^3.6.0",
"file-loader": "^6.0.0",
"html-webpack-plugin": "^4.3.0",
"image-webpack-loader": "^6.0.0",
"mini-css-extract-plugin": "^0.9.0",
"mocha": "^7.1.1",
"node-sass": "^4.14.1",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"terser-webpack-plugin": "^3.0.6",
"ts-loader": "^7.0.5",
"ts-mockito": "^2.5.0",
"ts-node": "^8.8.1",
"typescript": "3.8.3",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.12",
"jsdom-global": "^3.0.2"
},
"dependencies": {
"@fortawesome/fontawesome-free": "^5.13.1",
"bootstrap": "^4.3.1",
"bootstrap-table": "^1.16.0",
"file-saver": "^2.0.2",
"jquery": "^3.3.1",
"jquery-validation": "^1.17.0",
"jquery-validation-unobtrusive": "^3.2.11",
"moment": "^2.26.0",
"mpx-dialog": "^1.1.12",
"mpx-error-boundary": "^1.0.0",
"mpx-multi-language": "^1.0.8",
"mpx-react-page-components": "^1.0.3",
"mpx-utilities": "^1.0.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"tableexport.jquery.plugin": "^1.10.20"
},
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
]
},
"mocha": {
"extension": [
"ts",
"tsx"
],
"spec": "wwwroot/ts-tests/**/*.test.tsx",
"require": "wwwroot/ts-tests/babel-register.js"
},
"scripts": {
"test": "mocha",
"pack-dev": "webpack --mode development",
"pack-prod": "webpack --mode production",
"clean": "del /Q /F .\\wwwroot\\dist\\*.*"
}
}

我一直在尝试执行以下操作:
https://stackoverflow.com/a/45311836/4545812
对此事的任何帮助将不胜感激!

最佳答案

我设法解决了这个问题。最后我删除了 jsdom-global ,并添加 jsdom .我还添加了测试示例,以帮助在尝试使用 jsdom 和 typescript 进行测试时可能遇到类似问题的任何人。

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { act } from 'react-dom/test-utils';
import { expect } from 'chai';
import { TestApp } from '../ts/ExampleForTestingReact';
import { JSDOM } from 'jsdom'

declare global {
namespace NodeJS {
interface Global {
document: Document;
window: Window;
navigator: Navigator;
}
}
}

const { window } = new JSDOM('<!doctype html><html><body></body></html>');
global.document = window.document;
global.window = global.document.defaultView;


describe('App Component Testing', () => {
it('Renders Hello World Title', () => {
act(() => {
ReactDOM.render(<TestApp />, global.document);
});
const h1: Element = global.document.querySelector('h1');
expect(h1.textContent).to.equal('Hello World');
});
});
以上并不完美,因为我计划将以下部分移动到共享文件中,因为它将在每个 typescript 测试中使用
import { JSDOM } from 'jsdom'

declare global {
namespace NodeJS {
interface Global {
document: Document;
window: Window;
navigator: Navigator;
}
}
}

const { window } = new JSDOM('<!doctype html><html><body></body></html>');
global.document = window.document;
global.window = global.document.defaultView;
一个有用的 Stack Overflow 问题/答案帮助我解决了上述问题:
How to prevent "Property '...' does not exist on type 'Global'" with jsdom and typescript?

关于reactjs - 使用 Chai\Mocha 设置进行单元测试时,如何让 JSDOM 与 typescript 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62902689/

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