[英]Babel.config.js only for jest
we are using jest on a react typescript project.我们在 react typescript 项目上使用 jest。 If there is a babel.config.js with preset: [@babel/preset-env] the test running successfully.
如果有一个带有 preset: [@babel/preset-env] 的 babel.config.js 测试运行成功。 But with the existence of that babel file our next.js web project is not compiling any more.
但是随着那个 babel 文件的存在,我们的 next.js web 项目不再编译了。
How can I setup this babel.config.js only for jest and not for next.js?我如何设置这个 babel.config.js 只是为了开玩笑而不是为了 next.js?
See Making your Babel config jest-aware请参阅使您的 Babel 配置具有笑话意识
Jest will set process.env.NODE_ENV to 'test' if it's not set to something else.
Jest 会将 process.env.NODE_ENV 设置为 'test' 如果它没有设置为其他值。 You can use that in your configuration to conditionally setup only the compilation needed for Jest, eg
您可以在配置中使用它来有条件地仅设置 Jest 所需的编译,例如
babel.config.js
: babel.config.js
:
module.exports = api => {
const isTest = api.env('test');
// You can use isTest to determine what presets and plugins to use.
return {
// ...
};
};
An alternate solution to @slideshowp2 is to have a separate babelrc file dedicated to jest only. @slideshowp2的另一种解决方案是有一个单独的 babelrc 文件专用于开玩笑。
For example, you can call it babel.config.testing.js
then in your jest.config.js
file, you tell jest to use this dedicated babelrc file:例如,你可以将它命名为
babel.config.testing.js
然后在你的jest.config.js
文件中,你告诉 jest 使用这个专用的 babelrc 文件:
module.exports = {
transform: {
'\\.js$': ['babel-jest', { configFile: './Configuration/babel.config.testing.js' }]
},
};
Ref: https://github.com/facebook/jest/issues/3845#issuecomment-645298425参考: https://github.com/facebook/jest/issues/3845#issuecomment-645298425
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.