- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法让 Jest 的类型与 Webpack 5 和 TypeScript 一起工作,我已经尝试过其他解决方案来解决同样的问题。在下面的示例测试中,问题仅出在“屏幕”和“toBeInTheDocument”上。我倾向于它是 ESLint 配置问题。
我无法运行测试,它们失败了 Property 'toBeInTheDocument' does not exist on type 'JestMatchers<HTMLElement>'.
.我正在使用 yarn 3.1.0。我试过了 this answer 和许多其他的,包括通过 tsconfig.json
导入类型.我做错了什么?
示例测试(src/components/Example/spec/test.spec.tsx
):
import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { Example } from './Example';
import "@testing-library/jest-dom/extend-expect";
import "@testing-library/jest-dom";
test("Renders correctly", () => {
render(<Example />);
const example = screen.getByAltText("Example");
expect(example).toBeInTheDocument();
});
jest.config.js:
module.exports = {
// An array of directory names to be searched recursively up from the requiring module's location
moduleDirectories: ['node_modules', '<rootDir>/src'],
// A map to module names that allow stubbing out resources with a single module
moduleNameMapper: {
'\\.(css|less)$': '<rootDir>/__mocks__/styleMock.js',
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/__mocks__/fileMock.js',
},
// Preset for our Jest configuration base
preset: 'ts-jest/presets/js-with-ts',
setupFilesAfterEnv: ['<rootDir>/setupTests.ts'],
// Environment used for testing
testEnvironment: 'jsdom',
};
webpack.config.js
:
/* eslint-disable */
const { ModuleFederationPlugin } = require('webpack').container;
const path = require('path');
const packageJsonName = require('./package.json').name;
const packageJsonDeps = require('./package.json').dependencies;
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
module.exports = {
mode: 'development',
entry: path.resolve(__dirname, 'src', 'index.ts'),
devServer: {
client: {
overlay: true,
},
static: {
directory: './dist',
},
port: 3001,
historyApiFallback: true
},
module: {
rules: [
{
test: /\.(tsx|ts|jsx)?$/,
//exclude: /node_modules/,
use: [
{
loader: 'ts-loader',
options: {
experimentalWatchApi: true,
transpileOnly: true,
},
},
],
},
{
test: /\.js$/,
enforce: 'pre',
use: ['source-map-loader'],
},
],
},
output: {
path: path.resolve(__dirname, 'dist'),
publicPath: 'auto',
},
resolve: {
cacheWithContext: false,
extensions: ['.js', '.ts', '.tsx', '.jsx'],
plugins: [
new TsconfigPathsPlugin({
configFile: path.resolve(__dirname, './tsconfig.json'),
}),
],
symlinks: false,
},
};
package.json
:
{
"scripts": {
"start": "webpack-cli serve",
"build": "webpack --mode production",
"dev": "webpack serve --progress",
"test": "jest",
"test:coverage": "jest --coverage --watchAll=false",
"test:watch": "jest --watch"
},
"dependencies": {
"react": "17.0.2",
"react-router-dom": "^5.2.0",
},
"devDependencies": {
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "12.1.2",
"@testing-library/user-event": "13.5.0",
"@types/jest": "27.0.2",
"@types/react": "^17.0.33",
"@types/react-router-dom": "5.3.2",
"@typescript-eslint/eslint-plugin": "5.2.0",
"@typescript-eslint/parser": "4.33.0",
"eslint": "7.32.0",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-import": "2.25.2",
"eslint-plugin-jest": "25.2.2",
"eslint-plugin-react": "7.26.1",
"eslint-plugin-react-hooks": "4.2.0",
"jest": "^27.3.1",
"prettier": "2.4.1",
"ts-jest": "^27.0.7",
"ts-loader": "^9.2.6",
"tslib": "^2.3.1",
"typescript": "~4.3.5",
"webpack": "5",
"webpack-cli": "^4.9.1",
"webpack-dev-server": "^4.3.1"
}
}
和tsconfig.json
:
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"declaration": true,
"esModuleInterop": true,
"importHelpers": true,
"jsx": "react",
"lib": ["es6", "dom"],
"module": "esnext",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedParameters": true,
"outDir": "dist",
"resolveJsonModule": true,
"rootDir": ".",
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "es6"
},
"include": ["**/*.ts", "**/*.tsx", "**/*.jsx", "**/*.js"],
"exclude": [
"dist",
"node_modules"
]
}
和setupTests.ts
只是:import '@testing-library/jest-dom/extend-expect';
感谢您的指点。
最佳答案
安装类型使用:
npm i @types/testing-library__jest-dom -D
关于reactjs - Webpack 5 & Jest - 类型 'toBeInTheDocument' 上不存在属性 'JestMatchers<HTMLElement>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69737766/
我正在尝试为我的简单 React 应用程序编写测试,该应用程序使用 API 等为狗收容所创建 UI。我已经导入了如下所示的模块并运行了以下命令 npm install jest-dom react-t
正在尝试为组件库设置测试。我已经尝试了很多示例和所有类似的主题,但没有成功。 我的 setupTests.ts 文件被正确加载(通过 console.log 证明),并且库似乎可用,就像我添加 imp
这是我的工具提示代码,用于在 MouseOver 和 Mouse Out 上切换 CSS 属性 display: block display: none。 it('should show and h
我无法让 Jest 的类型与 Webpack 5 和 TypeScript 一起工作,我已经尝试过其他解决方案来解决同样的问题。在下面的示例测试中,问题仅出在“屏幕”和“toBeInTheDocume
我是一名优秀的程序员,十分优秀!