使用Parcel搭建的React项目中Jest遇意外token错误求助
Jest 无法解析JSX语法 - React + Parcel 测试问题
我用Parcel搭建了React应用,刚接触前端测试,在为ContactUs页面编写单元测试时遇到了Jest无法解析JSX的错误,试过StackOverflow上的多个解决方案都没用,现在卡壳了。
报错详情
● 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, refer to Jest docs for enabling instructions. • If you are trying to use TypeScript, refer to Jest docs for TypeScript setup. • 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 Jest docs. For information about custom transformations, refer to Jest docs on code transformation. Details: SyntaxError: /Users/vishnuvardhan/Desktop/ReactJs/src/components/__tests__/Contact.test.js: Support for the experimental syntax 'jsx' isn't currently enabled (8:12): 6 | 7 | test("Should load contact us component", () => { > 8 | render(<ContactUs/>) | ^ 9 | const heading = screen.getAllByRole('heading'); 10 | expect(heading).toBeInTheDocument(); 11 | })
我的配置文件
package.json
{ "name": "reactjs", "version": "1.0.0", "description": "This is a reactjs working environment for me!", "scripts": { "start": "parcel index.html", "build": "parcel build index.html", "test": "jest " }, "keywords": [ "Reactjs", "HTML", "CSS" ], "author": "Vishnu Vardhan", "license": "ISC", "devDependencies": { "@babel/core": "^7.25.2", "@babel/preset-env": "^7.25.4", "@testing-library/react": "^16.0.1", "babel-jest": "^29.7.0", "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", "parcel": "^2.12.0", "postcss": "^8.4.45", "process": "^0.11.10", "tailwindcss": "^3.4.10" }, "dependencies": { "@babel/preset-react": "^7.24.7", "@reduxjs/toolkit": "^2.2.7", "react": "^18.3.1", "react-dom": "^18.3.1", "react-redux": "^9.1.2", "react-router-dom": "^6.26.1" }, "browserslist": [ "last 2 versions" ] }
jest.config.js
/** @type {import('jest').Config} */ const config = { clearMocks: true, collectCoverage: true, coverageDirectory: "coverage", testEnvironment: "jsdom", }; module.exports = config;
babel.config.js
module.exports = { presets: [['@babel/preset-env', {targets: {node: 'current'}}]], };
.parcelrc
{ "extends": "@parcel/config-default", "transformers": { "*.{js,mjs,jsx,cjs,ts,tsx}": [ "@parcel/transformer-js", "@parcel/transformer-react-refresh-wrap" ] } }
内容的提问来源于stack exchange,提问作者Vishnu Vardhan Vankayalapati
相关产品推荐
相关产品推荐

