简体   繁体   English

Babel.config.js 仅用于开玩笑

[英]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.

相关问题 何时使用 babel.config.js 和 .babelrc - When to use babel.config.js and .babelrc babel.config.js 中的环境变量 - environment variables in babel.config.js webpack 忽略.babelrc 和 babel.config.js - webpack ignores .babelrc and babel.config.js npx babel 不从 babel.config.js 读取配置 - npx babel not reading configuration from babel.config.js babel.config.js 和 vue.config.js 有什么区别,我可以合并这两个文件吗? - what is the difference between babel.config.js and vue.config.js and can i combine the 2 files? 如何使Gulp在项目的根目录中读取Babel7的babel.config.js? - How to make Gulp read babel.config.js of Babel7 in the root of the project? 如果从软件包的依赖项安装了cli-plugin-babel,则不会加载babel.config.js - babel.config.js not loaded if cli-plugin-babel is installed from a package's dependencies api.cache(true)在Expo的babel.config.js中做什么? - What does api.cache(true) do in Expo's babel.config.js? 如何设置 babel.config.js,因此找不到相对于目录的预设“@babel/env”工作 - 反应 - How to setup babel.config.js, so Couldn't find preset "@babel/env" relative to directory works - react 尝试在 nextjs 应用程序中添加 `babel.config.js` 时出现`Cannot find module '.next\server\pages-manifest.json'` 错误 - Getting `Cannot find module '.next\server\pages-manifest.json'` error when trying to add `babel.config.js` in nextjs app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM