Jestjs Jest未在测试中传输ES6模块(SyntaxError:意外的令牌导出)

Jestjs Jest未在测试中传输ES6模块(SyntaxError:意外的令牌导出),jestjs,es6-modules,babel-jest,Jestjs,Es6 Modules,Babel Jest,我对如何理解ES6模块导入/导出语法感到非常兴奋,这阻碍了我的项目开发进度。我的项目结构如下: root module org-domain (ES6) org-services (ES6) react-ui-module (React via CRA2) { "env": { "test": { "presets": ["@babel/preset-flow", "@babel/preset-env"] "plugins": ["@babel/

我对如何理解ES6模块导入/导出语法感到非常兴奋,这阻碍了我的项目开发进度。我的项目结构如下:

root module
  org-domain (ES6)
  org-services (ES6)
  react-ui-module (React via CRA2)
{
  "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
在其包json中对
org-domain
具有本地路径依赖性:

// in org-services package.json

"dependencies": {
   "@org/domain": "file:../org-domain",
},
org services
中我的
.babelrc
如下所示:

root module
  org-domain (ES6)
  org-services (ES6)
  react-ui-module (React via CRA2)
{
  "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服务
的实现文件中,我正在导入
org域
,使用npm范围内的语法,如:
import。。。从'@org/domain

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

在本地开发中,当我尝试引用时,单击
@org/domain
,而不是直接指向
org services/node\u modules/@org/domain
,我会被重定向到实际的相对目录位置,即
根/org services
。我相信Jest忽略了
node\u模块
(如果我错了,请纠正我),但在我的
Jest.config.js
中,对于
org服务
,我有:

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
中)的所有配置都应该可以立即运行并将
“^.+\.js$”:“babel jest”
指令包含在
transform
下的
jest.config.js
项下,但它没有

关于这个问题,我在网上找到的每一样东西都试过了,但都没有成功。从那以后,我一直没有取得任何进展,我对这个测试框架失去了耐心,也没有对ES6的支持。应该不会这么难,所以很明显我在这里做错了什么。请告知

更新1 找到了另一个SO帖子,它反映了我现在的处境。


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

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

另外,您不需要在
jest.config.js
中定义
babeljest
转换,因为这是默认设置


希望这有帮助

您在这方面有什么进展吗?@42tg我无法解决这个问题。最后,在完全沮丧的情况下,我不得不将我单独的组织模块打包项目合并到一个单一的整体包中。我无法理解这样一个事实,即不支持开箱即用的多包项目设置。