我运行jest测试文件,并得到以下错误:
● 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/test/dev/pm/client/node_modules/spacetime/src/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import Spacetime from './spacetime.js'
^^^^^^
SyntaxError: Cannot use import statement outside a module
6 | import search from '../../assets/images/search.svg';
7 | import Dropdown from 'react-bootstrap/Dropdown';
> 8 | import spacetime from "spacetime";
| ^
9 | import languages from "../../libraries/languages/language-list";
10 | import { Rating } from 'react-simple-star-rating';
11 |
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/components/Mentor/MentorList.js:8:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 2.097 s
字符串
我试图通过将以下内容添加到package.json来修复它,但这不起作用:
"jest": {
"transformIgnorePatterns": [
"node_modules/(?!spacetime)"
]
},
型
有人能帮帮忙吗?
3条答案
按热度按时间cgvd09ve1#
spacetime
包的主文件是ES6模块。参见node_modules/spacetime/src/index.js
,这是包导出的主文件。时空的package.json包括"main": "src/index.js"
。默认情况下,transformers会忽略“node_modules”文件夹。
您不需要为jest config添加
transformIgnorePatterns
配置,transformers会忽略node_modules
文件夹。但这不是问题所在。
问题是jest默认情况下不会解析es6
import
/export
语句,即使它使用了babel。开箱即用的Jest支持Babel,它将用于根据Babel配置将文件转换为有效的JS。
我们需要转换es6
import
和export
的语法。选项1.添加babel配置
选项2.使用ts-jest预置jest。
jest.config.js
:字符串
选项3.使用esbuild-jest
jest.config.js
:型
bogh5gae2#
我不确定这个问题是否仍然相关。我看到的是你的
transformIgnorePattern
条目上有一个小错误。字符串
请注意
spacetime
周围的括号。我正在阅读Jest
文档中的transformIgnorePattern
部分,所以有一个新的眼睛,只要发现缺少的括号。这是一个很好的例子。
在下面的示例中,foo和bar的排除(也称为负先行Assert)相互抵消:
型
n6lpvg4x3#
我发现了一些可能导致这个问题的事情,并导致
transformIgnorePatterns
似乎不工作:transformIgnorePatterns
正则表达式的语法错误。你试图告诉jest忽略(即 * 不 * transform)* 几乎 *node_modules
中的所有内容,* 除了 * 问题依赖项,可能还有它的传递依赖项。所以这是一个复杂的正则表达式。我使用了在线正则表达式检查器来正确处理我的正则表达式。transform
,忘记包含默认的babel-jest
--config=
,所以我修改了错误的文件。这是一个愚蠢的错误,但我犯了这个错误-所以其他人也可能犯这个错误!babel.config.js
。它必须是babel.config.js
,.babelrc
不行,即使它应该是等价的。一旦我修复了所有这些问题,我发现错误四处移动,我不得不在正则表达式中添加一些额外的模块,因为它们是由原始依赖项导入的。