Jest与Vite进行测试

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

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

  1. {
  2. "name": "network-app",
  3. "private": true,
  4. "version": "0.0.0",
  5. "type": "module",
  6. "scripts": {
  7. "dev": "vite",
  8. "build": "vite build",
  9. "lint": "eslint src --ext js,jsx --report-unused-disable-directives --max-warnings 0",
  10. "preview": "vite preview",
  11. "test": "jest"
  12. },
  13. "dependencies": {
  14. "react": "^18.2.0",
  15. "react-dom": "^18.2.0"
  16. },
  17. "devDependencies": {
  18. "@testing-library/jest-dom": "^5.16.5",
  19. "@testing-library/react": "^14.0.0",
  20. "@testing-library/user-event": "^14.4.3",
  21. "@types/jest": "^29.5.1",
  22. "@types/react": "^18.0.28",
  23. "@types/react-dom": "^18.0.11",
  24. "@vitejs/plugin-react": "^4.0.0",
  25. "eslint": "^8.38.0",
  26. "eslint-plugin-react": "^7.32.2",
  27. "eslint-plugin-react-hooks": "^4.6.0",
  28. "eslint-plugin-react-refresh": "^0.3.4",
  29. "jest": "^29.5.0",
  30. "vite": "^4.3.2"
  31. }

App.test.jsx

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

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:

确定