Create React App后Jest配置缺失及测试报错问题排查
问题:Create React App 配置 Jest 后测试报错
用npx create-react-app创建项目后手动配置了Jest,运行npm test出现报错,以下是相关配置文件、测试代码及错误信息:
配置文件
package.json
{ "name": "sensorario", "version": "0.1.0", "private": true, "dependencies": { "@testing-library/jest-dom": "5.16.5", "@testing-library/user-event": "13.5.0", "react": "^18.2.0", "react-dom": "^18.2.0", "react-hook-form": "7.43.5", "react-query": "3.39.3", "react-router-dom": "6.8.2", "react-scripts": "2.1.3", "web-vitals": "2.1.4" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "jest", "eject": "react-scripts eject" }, "eslintConfig": { "extends": [ "react-app", "react-app/jest" ] }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] }, "devDependencies": { "@babel/preset-env": "7.21.4", "@babel/preset-react": "7.18.6", "@testing-library/react": "14.0.0", "babel-jest": "29.5.0", "jest": "27.5.1", "react-test-renderer": "18.2.0" }, "jest": { "setupFilesAfterEnv": [ "<rootDir>/src/setupTests.js" ] } }
babel.config.js
module.exports = { presets: [ '@babel/preset-env', ['@babel/preset-react', { runtime: 'automatic' }], ], };
jest.config.js
module.exports = { "testMatch": ["**/__test__/**"] }
测试代码
test/App.test.js
import { render, screen } from '@testing-library/react'; import App from '../src/App' test('renders learn react link', () => { render(<App />); const linkElement = screen.getByText(/learn react/i); expect(linkElement).toBeInTheDocument(); });
运行npm test后的报错信息
npm test | pbcopy git:master* npm● Multiple configurations found: * /Users/simonegentili/Development/github.com/sensorario/youtube.react/jest.config.js * `jest` key in /Users/simonegentili/Development/github.com/sensorario/youtube.react/package.json Implicit config resolution does not allow multiple configuration files. Either remove unused config files or select one explicitly with `--config`. Configuration Documentation: https://jestjs.io/docs/configuration.html FAIL __test__/App.test.js ● 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: /Users/simonegentili/Development/github.com/sensorario/youtube.react/src/App.css:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){.App { ^ SyntaxError: Unexpected token '.' > 1 | import './App.css'; | ^ 2 | import MyComponent from './MyComponent'; 3 | 4 | function App() { at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14) at Object.<anonymous> (src/App.js:1:1) Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 1.118 s Ran all test suites. youtube.react ➤ git:master*
疑问
已查阅React及Jest相关文档,想了解Babel与Jest的配置流程,是否可能因JSX未编译导致测试无法运行?
内容的提问来源于stack exchange,提问作者sensorario
相关产品推荐
相关产品推荐

