【问题标题】:Jest with Babel-jest not compiling js in parent folders玩 Babel-jest 不编译父文件夹中的 js
【发布时间】:2020-01-07 00:52:20
【问题描述】:

当我从一个文件夹运行 jest 并且某些文件位于父目录中时,我遇到了问题。

我的存储库看起来像这样:

|-common
|  |-.babelrc
|  |-src
|    |-js
|      |-someDependencies.js
|-app
   |-src
   | |-js
   |   |-someCodeIncludingSomeDependencies.js
   |-.babelrc
   |-package.json 
   |-webpack.config.js

我的 Jest 配置在 package.json 中并且是:

    "jest": {
        "moduleNameMapper": {
            "^vue$": "vue/dist/vue.common.js",
            "^@/(.*)$": "<rootDir>/src/$1",
            "^@Common/(.*)$": "<rootDir>/../common/src/$1"
        },
        "moduleFileExtensions": [
            "js",
            "vue"
        ],
        "snapshotSerializers": [
            "jest-serializer-vue"
        ],
        "transform": {
            "^.+\\.js$": "babel-jest",
            ".+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "jest-transform-stub",
            ".*\\.(vue)$": "vue-jest"
        },
        "collectCoverageFrom": [
            "**/*.{js,vue}",
            "!**/dist/**",
            "!<rootDir>/coverage/**",
            "!**/node_modules/**"
        ],
        "transformIgnorePatterns": [
            "<rootDir>/node_modules/"
        ]
    },

我从“app”文件夹运行 Jest。

我的问题是当我运行 Jest 时,'common/src/js' 中的代码没有被 babel 转换并出错:

...
SyntaxError: Unexpected token export

      3 | 
      4 | 
    > 5 | export default {}
        | ^
...

【问题讨论】:

    标签: javascript jestjs babel-jest


    【解决方案1】:

    我解决了将 '.babelrc' 配置文件更改为 'babel.config.js' 文件的问题...

    【讨论】:

      【解决方案2】:

      Babel 7.x 中的新功能,Babel 具有“根”目录的概念,默认为当前工作目录。

      更多信息你可以阅读here

      只需在.babelrc 文件中指定root 属性以指向父文件夹,或者您可以将.babelrc 文件移动到该父文件夹。

      【讨论】:

        猜你喜欢
        • 2018-06-11
        • 2018-12-29
        • 2020-05-13
        • 1970-01-01
        • 2018-09-22
        • 2019-08-15
        • 2018-07-07
        • 2018-05-21
        • 1970-01-01
        相关资源
        最近更新 更多