Jest无法转换node_modules

时间:2016-11-11 12:01:28

标签: unit-testing ecmascript-6 babeljs jestjs

运行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”标志。

1 个答案:

答案 0 :(得分:2)

我会将此标记为 答案,而不是 答案。

我有一个类似的问题,ES2015中node_modules中的多个组件,需要进行转换。

我将transformIgnorePatterns更新为:

["/node_modules/(?!(our-react-components-.*?\\.js$))"]

our-react-components-*个文件夹中模块的负向预测意味着Jest(因此Babel)会找到并转换我们的共享组件。

这是在Jest 20.0.4的一个弹出的Create React App项目中。