Jest与Vite进行测试

huangapple go评论82阅读模式
英文:

Jest testing with vite

问题

I'm facing a problem with my test code. In my App.test.jsx file, "test" and "expect" are not defined.

Package.json

{
"name": "network-app",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint src --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"test": "jest"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3",
"@types/jest": "^29.5.1",
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^4.0.0",
"eslint": "^8.38.0",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.4",
"jest": "^29.5.0",
"vite": "^4.3.2"
}
}

App.test.jsx

test('Renders main page correctly', () => {
expect(true).toBeTruthy();
});

How to solve this issue?

英文:

I'am facing a problem with my test code. In my App.test.jsx file test and expect is not defined

Package.json

{
  "name": "network-app",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
       "dev": "vite",
       "build": "vite build",
       "lint": "eslint src --ext js,jsx --report-unused-disable-directives --max-warnings 0",
       "preview": "vite preview",
       "test": "jest"
  },
  "dependencies": {
       "react": "^18.2.0",
       "react-dom": "^18.2.0"
  },
  "devDependencies": {
       "@testing-library/jest-dom": "^5.16.5",
       "@testing-library/react": "^14.0.0",
       "@testing-library/user-event": "^14.4.3",
       "@types/jest": "^29.5.1",
       "@types/react": "^18.0.28",
       "@types/react-dom": "^18.0.11",
       "@vitejs/plugin-react": "^4.0.0",
       "eslint": "^8.38.0",
       "eslint-plugin-react": "^7.32.2",
       "eslint-plugin-react-hooks": "^4.6.0",
       "eslint-plugin-react-refresh": "^0.3.4",
       "jest": "^29.5.0",
       "vite": "^4.3.2"
 }

App.test.jsx

 test('Renders main page correctly', () => {
     expect(true).toBeTruthy();
 });

How to solve this issue?

答案1

得分: 1

在Jest的安装指南中,指出Vite不支持Jest,因为它有不同的插件系统。有一个名为vite-jest的库可以轻松安装和配置。

这种情况下,Jest文档可能会有所帮助:
Jest在Vite中的安装

这是Vite-Jest库的链接:Vite-Jest NPM库

英文:

In jest's installation guide, it's indicated that vite doesn't support jest due to it's different plugin system. There's a library known as vite-jest which can be installed and configured easily.

The Jest documentation can be helpful in this case:
Jest Installation in Vite

Here's the link to the Vite-Jest library: Vite-Jest NPM Library

答案2

得分: 1

你可以使用 vitest。这是一个由 Vite 驱动的单元测试框架。

英文:

Alternativerly you can use vitest. A unit test framework powered by Vite

huangapple
  • 本文由 发表于 2023年5月28日 20:40:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定