我尝试使用Jest在React应用程序中进行测试;我的应用程序使用Vite作为模块编译器。问题是,每次我运行测试时,我都会得到以下错误:
> [email protected] test
> jest
FAIL src/test/App.test.jsx
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to
enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
SyntaxError: C:\Users\Tomas\Desktop\react-test\src\test\App.test.jsx: Support for the experimental syntax 'jsx' isn't currently enabled (8:30):
6 |
7 | test("renders content", ()=>{
> 8 | const component = render(<App></App>)
| ^
9 | console.log(component)
10 | })
Add @babel/preset-react (https://git.io/JfeDR) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://git.io/vb4yA) to the 'plugins' section to enable parsing.
at Parser._raise (node_modules/@babel/parser/src/parser/error.js:150:45)
at Parser.raiseWithData (node_modules/@babel/parser/src/parser/error.js:145:17)
at Parser.expectOnePlugin (node_modules/@babel/parser/src/parser/util.js:214:18)
at Parser.parseExprAtom (node_modules/@babel/parser/src/parser/expression.js:1238:16)
at Parser.parseExprSubscripts (node_modules/@babel/parser/src/parser/expression.js:682:23)
at Parser.parseUpdate (node_modules/@babel/parser/src/parser/expression.js:662:21)
at Parser.parseMaybeUnary (node_modules/@babel/parser/src/parser/expression.js:633:23)
at Parser.parseMaybeUnaryOrPrivate (node_modules/@babel/parser/src/parser/expression.js:388:14)
at Parser.parseExprOps (node_modules/@babel/parser/src/parser/expression.js:398:23)
at Parser.parseMaybeConditional (node_modules/@babel/parser/src/parser/expression.js:356:23)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.379 s
Ran all test suites.
字符串
Package.json
{
"name": "react-test",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"test": "jest"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"jest": {
"verbose": true,
"testEnvironment": "jsdom",
"transform": {
"^.+\\.(js|jsx)$": "babel-jest"
},
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleNameMapper": {
"\\.(gif|ttf|eot|svg|png)$": "<rootDir>/test/__mocks__/fileMock.js",
"\\.(css|less|sass|scss)$": "identity-obj-proxy"
}
},
"devDependencies": {
"@babel/plugin-syntax-jsx": "^7.16.7",
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/react": "^12.1.3",
"@types/jest": "^27.4.1",
"@vitejs/plugin-react": "^1.0.7",
"jest": "^27.5.1",
"vite": "^2.8.0"
}
}
型
App.test.jsx
import React from "react";
import "@testing-library/jest-dom/extend-expect"
import { render } from "@testing-library/react";
import App from "../App.jsx";
test("renders content", ()=>{
const component = render(<App></App>)
console.log(component)
})
型
2条答案
按热度按时间qc6wkl3g1#
错误输出是正确的,Jest在本地节点二进制文件上运行,并要求将jsx文件转换为它可以理解的语法。
默认情况下,Vite不会进行这种转换。它的设计目的是将您的代码转换并捆绑到适合浏览器的
bundle.js
中(它可以进行其他类型的输出,如库。您需要调整vite.config.js
)。幸运的是,其他人已经为您解决了这个问题,我推荐使用vitest,因为您不需要下载另一个transformer Transformer,也不需要花费大量时间来设置复杂的构建脚本。
以下是如何快速设置它的指南:
https://www.eternaldev.com/blog/testing-a-react-application-with-vitest/的
迁移指南:
https://vitest.dev/guide/migration.html的
ulmd4ohb2#
但对我来说,在目前的阶段,它似乎工作。
npm
而不是pnpm
,只需将pnpm add
替换为npm install
,它应该可以工作)*过程
设置Vite
选择React Typescript
安装Jest & React测试库
字符串
在
proj-test-vitejest/src
下创建主测试文件Greet.test.tsx
,确保你
import '@testing-library/jest-dom';
型
现在打印脚本eslint,intellisense,import,应该都很好
此时无需手动更改
tsconfig.json
或package.json
你应该能够找到方法
toBeInTheDocument()
在H:\Using\proj-test-vitejest\node_modules\.pnpm\@ [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection) _@ [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection) [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection) \node_modules\@testing-library\jest-dom\types\matchers.d.ts
此时不需要其他程序,如
"types": ["node", "jest", "@testing-library/jest-dom"],
setupFilesAfterEnv: [ "<rootDir>/support/setupTests.js" ],
个npm install @types/testing-library__jest-dom -D
运行
npx jest
,将失败型
所以
创建
jest.config.ts
将以下内容添加到
jest.config.ts
型
Babel throwing Support for the experimental syntax 'jsx' isn't currently enabled
npx jest
再试一次,失败,型
所以
型
npx jest
再试一次,失败,所以
将以下内容添加到
jest.config.ts
npx jest
再试一次,你现在成功了。结果为(1次成功,1次失败(故意))
其他
如果你厌倦了每个测试文件中的
import '@testing-library/jest-dom';
,你可以在
Greet.test.tsx
中注解它型
在
proj-test-vitejest/src
下创建jest.setup.ts
,将以下内容添加到
jest.config.ts
在
proj-test-vitejest/src
下创建Globals.d.ts
,将以下内容添加到
tsconfig.json
Property 'toBeInTheDocument' does not exist on type 'Matchers'
概述
package.json
jest.config.ts
tsconfig.json
的数据