我运行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)"
]
},有人能帮忙吗?
发布于 2022-02-10 06:03:45
spacetime包的主文件是ES6模块。请参阅node_modules/spacetime/src/index.js,这是主要文件,空间时间包含"main": "src/index.js"的包exports.The package.json。
默认情况下,变压器忽略"node_modules“文件夹。
您不需要为jest配置添加transformIgnorePatterns配置,node_modules文件夹将被转换器忽略。
但这不是问题所在。
问题是,在默认情况下,jest不解析es6 import/export语句,即使它使用babel。
开箱即用的Jest支持Babel,它将用于根据Babel配置将您的文件转换为有效的JS。
我们需要转换es6、import和export语法。
备选方案1. 添加babel配置
选项2.使用T-玩笑预置作为玩笑。
jest.config.js
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'jsdom'
};选项3.使用埃斯博特
jest.config.js
module.exports = {
testEnvironment: 'jsdom',
transform: {
'\\.[jt]sx?$': 'esbuild-jest',
},
};发布于 2022-09-12 15:40:34
我不知道这个问题是否仍然相关。我看到的是,您的transformIgnorePattern条目上有一个小错误。
"jest": {
"transformIgnorePatterns": [
"node_modules/(?!(spacetime)/)"
]
}请注意spacetime周围的括号。我正在阅读transformIgnorePattern文档中的部分Jest部分,因此,有了一只新的眼睛,只需找出缺少的括号。
这是Jest Docs的引语
在下面的示例中,foo和bar的排除(也称为负前瞻性断言)相互取消: { "transformIgnorePatterns":“节点_模块/(?!foo/)”,>“节点_模块/(?!bar/)”//不是您想要的}`
https://stackoverflow.com/questions/71058618
复制相似问题