运行 Jest 时出现以下错误
SyntaxError: node_modules/my_styleguide/src/components/blocks/SegmentSetting.jsx: Unexpected token (46:10)
44 | ) {
45 | return (
> 46 | <p className="SettingDescr">{self.props.description}</p>
| ^
47 | )
48 | }
49 | }
node_modules 依赖仍然是它的 es6 格式。 Jest 似乎没有提供转译您的 node_modules 的选项,就像它对您的实际应用程序所做的那样。相反,jest 会忽略 node_modules 文件夹。
.bashrc 文件在我看来没问题:
{
"presets": ["es2015", "react", "stage-0"]
}
如何让 Jest 也转译您的 node_modules?这相当于我们在 mocha 中使用的“--ignore false”标志。
最佳答案
我会将此标记为一个答案,而不是答案。
我有一个类似的问题,ES2015 中的 node_modules 中有多个组件需要转译。
我将 transformIgnorePatterns
更新为:
["/node_modules/(?!(our-react-components-.*?\\.js$))"]
our-react-components-*
文件夹中模块的负面前瞻性意味着 Jest(以及 Babel)将找到并转换我们的共享组件。
这是在使用 Jest 20.0.4 弹出的 Create React App 项目中。
关于unit-testing - Jest 无法转换 node_modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40547618/