如何在Jest中使用babel-preset-env [英] How to use babel-preset-env with Jest

查看:545
本文介绍了如何在Jest中使用babel-preset-env的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在更新API,Babel的Henry Zhu提醒我使用此名为babel-preset-env的预设,以替换对babel-preset-es2015babel-preset-es2018的需要.

We are in the midst of updating our API, and Henry Zhu from Babel alerted me to this preset called babel-preset-env to replace need for babel-preset-es2015 and babel-preset-es2018.

现在,我在理解处理所有事物的最简单方法时遇到了困难.

Now, I am encountering difficulty understanding the simplest way to handle everything.

  • 我们的API使用节点v8.x和async/await,原生的诺言
  • 我想要传播算子
  • 我想要管道运算符
  • 我想要导入/导出语法
  • 我要支持Jest
  • 我喜欢babel-node如何将API转换到内存中

如果我仅向您显示配置的当前位置,这会更容易:

This will be easier if I just show you the current position of our config:

.babelrc

.babelrc

 {
   "presets": [
     "env",
       {
         "targets": {
           "node": "current"
         }
       },
     "jest"
   ]
 }

package.json

package.json

 {
   "scripts": {
     "test": "node --harmony-async-await node_modules/jest/bin/jest.js",
     "start:local": "NODE_ENV=localhost npm run babel-node -- warpcore/server.js",
     "start": "npm run babel-node -- warpcore/server.js",
     "babel-node": "babel-node --presets=es2015,stage-2"
   },
   "dependencies": {
     "babel-polyfill": "^6.23.0"
   },
   "devDependencies": {
     "babel-cli": "^6.24.1",
     "babel-core": "^6.25.0",
     "babel-eslint": "^7.2.3",
     "babel-jest": "^20.0.3",
     "babel-preset-env": "^1.6.0",
     "babel-preset-es2015": "^6.24.1",
     "babel-preset-es2018": "^1.0.0",
     "babel-preset-stage-2": "^6.24.1",
     "jest": "^20.0.4"
   },
   "jest": {
     "testURL": "http://localhost:8080",
     "testEnvironment": "node"
   }
 }

我不确定如何组织这些事情以最好地实现上面的项目符号列表.

I am uncertain how these things need to be organized to best achieve my bullet list above.

我应该进行哪些更改?

What changes should I make?

  • 我认为babel-node脚本需要更改
  • 我怀疑我可以删除其中一些软件包
  • 我怀疑.babelrc文件不是最佳
    • I think the babel-node script needs to change
    • I suspect I can remove some of these packages
    • I suspect the .babelrc file isn't optimal
    • 推荐答案

      我认为我可以使用它.解决方法如下:

      I think I got it working. Here is the solution:

      .babelrc

      .babelrc

      该问题中发布的一个语法错误,因为环境预设需要用括号括起来[](来自: http://babeljs.io/docs/plugins/preset-env/)

      The one posted in the question has a syntax error because the env preset needs to be wrapped in brackets[] (from: http://babeljs.io/docs/plugins/preset-env/)

      正确:

       {
         "presets": [
           ["env",
             {
               "targets": {
                 "node": "current"
               }
             }],
           "jest"
         ]
       }
      

      package.json

      package.json

      问题中发布的内容中有一些可以删除的内容:

      The one posted in the question has a few things that can be removed:

        {
           "scripts": {
             "test": "jest --verbose",
             "start:local": "cross-env NODE_ENV=localhost babel-node -- app.js",
             "babel-node": "babel-node --presets=env"
          },
          "dependencies": {
            "babel-cli": "^6.24.1",
            "babel-preset-env": "^1.6.0"
          },
          "devDependencies": {
            "babel-eslint": "^7.2.3",
            "babel-jest": "^20.0.3",
            "jest": "^20.0.4"
          },
          "jest": {
            "testURL": "http://localhost:8080",
            "testEnvironment": "node"
          }
        }
      

      我认为清洁得多.如果要显式包含或排除任何预设,或指定要支持的浏览器,则可以从.babelrc文件调制预设.

      Much cleaner in my opinion. You can modulate the presets from the .babelrc file if you want to explicitly include or exclude any, or specify which browsers to support.

      这篇关于如何在Jest中使用babel-preset-env的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆