gpt4 book ai didi

jestjs - Jest 在测试中未转译 ES6 模块(SyntaxError : Unexpected token export))

转载 作者:行者123 更新时间:2023-12-02 09:47:31 30 4
gpt4 key购买 nike

我正处于让 Jest 理解 ES6 模块导入/导出语法的沸点,它阻碍了我的项目开发进度。我的项目结构如下:

root module
org-domain (ES6)
org-services (ES6)
react-ui-module (React via CRA2)

org-services 在其包 json 中对 org-domain 有本地路径依赖:

// in org-services package.json

"dependencies": {
"@org/domain": "file:../org-domain",
},

我在 org-services 中的 .babelrc 如下:

{
"env": {
"test": {
"presets": ["@babel/preset-flow", "@babel/preset-env"]
"plugins": ["@babel/plugin-transform-modules-commonjs"]
}
},
"presets": [
"@babel/preset-flow",
["@babel/preset-env", {
"targets": {
"esmodules": true
}
}]
],
"plugins": [
["module-resolver", {
"root": ["./node_modules/@org/domain"],
"alias": {
"@org/constants": "./node_modules/@org/domain/src/constants",
"@org/contracts": "./node_modules/@org/domain/src/request-contracts"
}
}]
]
}

我不知道问题是否是由于我包含依赖项的方式所致,因此为了清楚起见,我将添加与这些模块的导入/导出相关的任何内容的更详细信息。

org-services 的实现文件中,我使用 npm 作用域语法导入 org-domain,如下所示: import ... from '@org/域名

以下是我的一些观察结果:

在本地开发中,当我尝试引用单击@org/domain时,我被重定向到org-services/node_modules/@org/domain,而不是被定向到org-services/node_modules/@org/domain到实际的相对目录位置,即 root/org-services。我相信 Jest 忽略了 node_modules (如果我错了,请纠正我),但在我的 org-servicesjest.config.js 中,我有:

collectCoverage: true,
coverageDirectory: 'coverage',
coveragePathIgnorePatterns: [
'/node_modules/'
],
moduleDirectories: [
'src',
'node_modules'
],
moduleFileExtensions: [
'js'
],
transform: {
'^.+\\.js$': 'babel-jest'
},
transformIgnorePatterns: [
'node_modules/(?!@org/domain)$'
]

据我了解,现在一切都应该可以使用我在测试中设置插件 @babel/plugin-transform-modules-commonjs 的所有配置(在 . babelrc),并在 jest 中的 transform 键下包含 '^.+\\.js$': 'babel-jest' 指令。 config.js 位于 org-services 下——但事实并非如此。

我已经尝试了网上能找到的关于此问题的所有方法,但没有成功。从那以后我一无所获,我对这个测试框架和缺乏对 ES6 的支持失去了耐心。它不应该这么难,所以显然我在这里做错了什么。请指教。

更新 1

发现另一篇 SO 帖子反射(reflect)了我所处的情况。 Jest fails to transpile import from npm linked module

不幸的是,提供的解决方案不适用于我的情况。

最佳答案

从 @babel/xyz: 7.0.0 升级到 7.1.2 后,我开始收到有关“导入”的错误

Jest encountered an unexpected token

<snip>

Details:

C:\....\SstcStrategy.test.js:2
import sequelizeFixtures from 'sequelize-fixtures';
^^^^^^

SyntaxError: Unexpected token import

at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)

要解决此问题,我必须添加 @babel/plugin-transform-modules-commonjs 正如您在问题中提到的。

我的 babel.config.js 现在看起来像这样:

module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: '8.10',
},
debug: false,
},
],
],
ignore: ['node_modules'],
plugins: [
'@babel/plugin-transform-runtime',
'@babel/plugin-transform-modules-commonjs',

// Stage 2
['@babel/plugin-proposal-decorators', { legacy: true }],
'@babel/plugin-proposal-function-sent',
'@babel/plugin-proposal-export-namespace-from',
'@babel/plugin-proposal-numeric-separator',
'@babel/plugin-proposal-throw-expressions',

// Stage 3
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-syntax-import-meta',
['@babel/plugin-proposal-class-properties', { loose: false }],
'@babel/plugin-proposal-json-strings',
],
};

顺便说一句,您不需要在 jest.config.js 中定义 babel-jest 转换,因为这是默认设置。

希望这有帮助

关于jestjs - Jest 在测试中未转译 ES6 模块(SyntaxError : Unexpected token export)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52633286/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com