Jest无法从npm链接模块转换导入 [英] Jest fails to transpile import from npm linked module
问题描述
我有一个包含多个模块的项目(使用 Lerna ),我想使用Jest来运行测试。但是,当我测试使用共享模块的代码(通过Lerna的npm链接模块)时,似乎Babel未正确应用,我收到以下错误:
I have a project with multiple modules (using Lerna) and I want to use Jest to run tests. However, when I test code that uses a shared module (npm linked module via Lerna) it seems that Babel is not correctly applied and I get the following error:
SyntaxError: Unexpected token import
我的项目结构如下:
- my-project
|- shared
|- native
|- web
web
和 native
需要共享
模块。当我进入共享
目录并在Jest中运行本地测试时,一切正常。如果我在 web
目录中运行Jest测试,只要我包含来自 shared
的内容,就会出现上述错误。
web
and native
require the shared
module. When I go into the shared
directory and run the local tests in Jest everything works fine. If I run Jest tests in the web
directory the above error occurs as soon as I include something from shared
.
这是一个导致错误的超级简单测试:
Here is a super simple test that causes the error:
import { util } from 'shared';
it('returns false if not prod', () => {
expect(util.isProd()).toBe(false);
});
我的 .babelrc
如下所示:
{
"presets": [
"env",
"flow",
"react"
],
"plugins": [
"flow-react-proptypes",
"transform-object-rest-spread",
"transform-class-properties"
]
}
我尝试了一切我可以找到,包括:
I tried everything I could find, including:
- 不同的Babel配置,包括一个带有
es2015
为测试环境预设并启用模块
- 手动设置
转换
选项for babel-jest - 如上所述,Jest可以在
共享
模块中执行,因此,Jest和babel-jest
也安装在那里。
- Different Babel configs, including one with the
es2015
preset and enablingmodules
for the test environment - Manually setting the
transform
option for babel-jest - As mentioned, Jest can be executed in the
shared
module, thus, Jest andbabel-jest
are installed there as well.
推荐答案
我必须在<更改Jest的 transformIgnorePatterns
配置选项code> package.json web
模块。
I had to change the transformIgnorePatterns
configuration option of Jest in the package.json
of the web
module.
{
"jest": {
"transformIgnorePatterns": [
"<rootDir>/node_modules/(?!shared|another)"
]
},
}
这篇关于Jest无法从npm链接模块转换导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!