babel @ 7和jest配置 [英] babel@7 and jest configuration

查看:530
本文介绍了babel @ 7和jest配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许您可以帮助我吗? 我尝试将玩笑配置为使用babel @ 7 所以我有:

"jest": "^23.4.1",
"@babel/core": "^7.0.0-beta.54",
"babel-7-jest": "^21.3.3",
"babel-jest": "^20.0.3",

在package.json中有有趣的配置

"jest": {
    "transform": {
      "^.+\\.js$": "babel-7-jest",
    },

得到了

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string

但是如果我使用

"jest": {
    "transform": {
      "^.+\\.js$": "babel-jest",
    },

我知道了

Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel.

babel config: https://gist.github.com/SilentImp/1506e9c26d16d9839a4469c6f3ae5c4d >

也许您有一些想法?

解决方案

我相信我已经找到了可行的解决方案(感谢Jest团队提供了破碎的文档并绕过此问题逃避了GitHub问题).

package.jsondevDependencies部分中需要以下内容:

  "devDependencies": {
    "@babel/core": "^7.0.0-beta.54",
    "@babel/preset-env": "^7.0.0-beta.54",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^23.4.0",
    "bili": "^3.1.2",
    "jest": "^23.4.1",
    "regenerator-runtime": "^0.12.0"
  }

.babelrc中的以下内容:

{
    "presets": [
        [
            "@babel/preset-env",
            {
                "debug": false,
                "targets": {
                    "browsers": [
                        "last 3 versions"
                    ]
                }
            }
        ]
    ]
}

在我的特定项目中,我不需要使用Jest配置,因此我删除了空的jest.config.js文件.

要点:

  • 删除babel-7-jest,因为现在已经不推荐使用了官方支持为此.
  • 请确保仅继续使用@babel/xyz软件包-我安装的babel-core桥接器是使用最新Babel 7的官方"方式.我想这种需求将来会在某个时候消除.一切都迁移到Babel 7.
  • 您现在可以使用包括import/export在内的ES6 +功能,而不再需要过时的require().

如果您想获得通过/失败测试的更详细的日志,请将其放入您的jest.config.js:

module.exports = {
    "verbose": true   
}

Maybe you may help me? I try to configure jest to use babel@7 So I have:

"jest": "^23.4.1",
"@babel/core": "^7.0.0-beta.54",
"babel-7-jest": "^21.3.3",
"babel-jest": "^20.0.3",

And jest config inside package.json

"jest": {
    "transform": {
      "^.+\\.js$": "babel-7-jest",
    },

And got

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string

But if I use

"jest": {
    "transform": {
      "^.+\\.js$": "babel-jest",
    },

I got

Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "babel-core" to see what is calling Babel.

babel config: https://gist.github.com/SilentImp/1506e9c26d16d9839a4469c6f3ae5c4d

Maybe you have some ideas?

解决方案

I believe I have found a working solution (no thanks to the Jest team providing broken documentation and evading GitHub issues around this issue).

You need the following in your devDependencies section of your package.json:

  "devDependencies": {
    "@babel/core": "^7.0.0-beta.54",
    "@babel/preset-env": "^7.0.0-beta.54",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^23.4.0",
    "bili": "^3.1.2",
    "jest": "^23.4.1",
    "regenerator-runtime": "^0.12.0"
  }

The following in your .babelrc:

{
    "presets": [
        [
            "@babel/preset-env",
            {
                "debug": false,
                "targets": {
                    "browsers": [
                        "last 3 versions"
                    ]
                }
            }
        ]
    ]
}

In my particular project I did not need to use the Jest config so I deleted my empty jest.config.js file.

Key points:

  • Remove babel-7-jest as this is deprecated as there is now official support for it.
  • Make sure to only use @babel/xyz packages going forward - the babel-core bridge one I have installed is the "official" way to use latest Babel 7. I imagine this need will be removed at some point in the future as everything migrates to Babel 7.
  • You can now use ES6+ features including import/export and no longer need the antiquated require().

Edit:

If you want to have a more detailed log of passing/failing tests then put this in your jest.config.js:

module.exports = {
    "verbose": true   
}

这篇关于babel @ 7和jest配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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