Reactjs 在使用jest进行测试时,将babel配置为在node_模块内编译特定的包

Reactjs 在使用jest进行测试时,将babel配置为在node_模块内编译特定的包,reactjs,jestjs,babeljs,Reactjs,Jestjs,Babeljs,默认情况下,babel跳过node_模块中的所有内容。但我在/nodemodules/special module下有一个模块,它是jsx,需要编译 在构建主应用程序时,我在webpack.config.json中解决了这个问题,方法是将其包含在规则数组中: { test: /\.jsx?$/, exclude: /node_modules(?!\/(special-module))/, loader: 'babel-loader', query: {presets: ['es2

默认情况下,babel跳过
node_模块中的所有内容。
但我在
/nodemodules/special module
下有一个模块,它是jsx,需要编译

在构建主应用程序时,我在
webpack.config.json
中解决了这个问题,方法是将其包含在
规则
数组中:

{
  test: /\.jsx?$/,
  exclude: /node_modules(?!\/(special-module))/,
  loader: 'babel-loader',
  query: {presets: ['es2015', 'react']}  // to transform JSX into JS
}
但当使用jest运行测试时,不会使用webpack。我得到一个错误,显示特殊模块没有被编译

.../myproject/node_modules/special-module/src/Special.jsx:151
                <div>
                ^    
SyntaxError: Unexpected token <

我的理解是,提供
ignore
键可以防止默认排除
node_模块
,只忽略与给定正则表达式匹配的文件。但上述措施似乎并不奏效。
特殊模块
仍未编译。这是在使用Bable6.23.0,所以应该不是问题。

最终解决了这个问题。它需要进入
package.json中的
transformIgnorePatterns

  "jest": {
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!special-module)"
    ]
    ...
  }
“开玩笑”:{
“transformIgnorePatterns”:[
“/node_modules/(?!特殊模块)”
]
...
}
事实上,我发现我根本不需要一个
.babelrc
,就像
babeljest
一样

  "jest": {
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!special-module)"
    ]
    ...
  }