当我尝试为我的React组件运行单元测试时,我遇到了这个错误。我使用VSCode,所以我注意到当我编写expect().toBe(..)时,它不会自动提示该术语,所以我认为我的文件有问题。我在另一个项目上进行了测试,自动建议运行得很好。我只使用一个基本的true断言来测试测试文件是否工作。
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.
• 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/daniel/Documents/GitHub/flinder-react/src/components/FirstStep.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import React from 'react';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest/node_modules/jest-runtime/build/index.js:1479:14)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.311 s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! flinder-react@0.1.0 test: `jest`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the flinder-react@0.1.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/daniel/.npm/_logs/2021-09-03T23_14_44_337Z-debug.log
daniel@daniels-Air flinder-react % npm run test
> flinder-react@0.1.0 test /Users/daniel/Documents/GitHub/flinder-react
> jest
FAIL src/components/FirstStep.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.
• 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: /Users/daniel/Documents/GitHub/flinder-react/src/components/FirstStep.test.js: Support for the experimental syntax 'jsx' isn't currently enabled (10:33):
8 | describe('FirstStep', () => {
9 | it('button is disabled', () => {
> 10 | const wrapper = shallow(<FirstStep />);
| ^
11 | const button = wrapper.find("Button");
12 |
13 | expect(button.props().disabled).toBe(true);
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:134:45)
at Parser.raiseWithData (node_modules/@babel/parser/src/parser/error.js:129:17)
at Parser.expectOnePlugin (node_modules/@babel/parser/src/parser/util.js:198:18)
at Parser.parseExprAtom (node_modules/@babel/parser/src/parser/expression.js:1275:18)
at Parser.parseExprSubscripts (node_modules/@babel/parser/src/parser/expression.js:652:23)
at Parser.parseUpdate (node_modules/@babel/parser/src/parser/expression.js:632:21)
at Parser.parseMaybeUnary (node_modules/@babel/parser/src/parser/expression.js:604:23)
at Parser.parseExprOps (node_modules/@babel/parser/src/parser/expression.js:374:23)
at Parser.parseMaybeConditional (node_modules/@babel/parser/src/parser/expression.js:340:23)
at Parser.parseMaybeAssign (node_modules/@babel/parser/src/parser/expression.js:279:21)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.395 s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! flinder-react@0.1.0 test: `jest`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the flinder-react@0.1.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/daniel/.npm/_logs/2021-09-03T23_17_16_517Z-debug.log发布于 2021-09-04 00:17:38
您正在尝试使用模块语法,但默认情况下Jest使用commonjs语法。
SyntaxError: Cannot use import statement outside a moduleJest支持es模块,但目前它还处于实验阶段,并不是很稳定。您现在可以使用babel-jest来转换您的代码。在您的jest配置中,添加一个转换定义并使用babel预设。阅读更多关于它的内容:https://www.npmjs.com/package/babel-jest和jest的“入门”指南:https://jestjs.io/docs/getting-started
https://stackoverflow.com/questions/69051384
复制相似问题