玩笑测试(使用babel)无效令牌导入

时间:2018-12-01 08:18:59

标签: javascript jestjs babel

使用Jest测试Firebase功能,我在规格文件中的导入..遇到了麻烦。

console.log

 import 'jest';
    ^^^^^^

    SyntaxError: Unexpected token import

我正在使用@ babel / core和@ babel / preset-env和babel-jest

package.json

"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/preset-env": "^7.1.6",
"@types/jest": "^23.3.10",
"babel-jest": "^23.6.0",
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0",
"firebase-functions-test": "^0.1.5",
"jest": "^23.6.0",
"ts-jest": "^23.10.5"
},
"engines": {
"node": "8"
},
"private": true,
"jest": {
"verbose": true,
"transform": {
  "^.+\\.jsx?$": "babel-jest",
  "^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
  "ts",
  "tsx",
  "js",
  "jsx",
  "json",
  "node"
  ]
}

然后我创建了一个babel.config.js文件

babel.config.js

  module.exports = {
    presets: [
      [
        "@babel/preset-env"
      ]
    ]
  };

我想念什么? 感谢您的反馈

更新

我按照Jest Using Babel doc ...(v 7)中的说明更新了配置,

console.log

    Cannot find module 'babel-preset-env' from '/Users/yves/Developments/WIP/VUE.JS-cli-3/4-chocha-home-cloudFunctions/chocha/functions'
        - Did you mean "@babel/env"?

package.json

    "devDependencies": {
        "@babel/core": "^7.1.6",
        "@babel/preset-env": "^7.1.6",
        "@types/jest": "^23.3.10",
        "babel-core": "^7.0.0-bridge.0",
        "babel-jest": "^23.6.0",
        "eslint": "^4.12.0",
        "eslint-plugin-promise": "^3.6.0",
        "firebase-functions-test": "^0.1.5",
        "jest": "^23.6.0",
        "regenerator-runtime": "^0.13.1",
        "ts-jest": "^23.10.5"
      },

babel.config.js

    module.exports = {
    presets: [
    ["@babel/env"]
    ]
    };

.babelrc

    {
      "presets": ["env"]
    }

但是我仍然遇到一个错误...对于初学者而言,尚不清楚应实际使用的内容...文档中的建议过多... 我只将JS用于Firebase函数,没有React,没有Vue ...

更新

我更新了.babelrc

    {
      "presets": ["@babel/preset-env"]
    }

现在我又遇到了另一个Babel错误...

    tests/index.spec.js
      ● Test suite failed to run

        TypeError: programPath.hub.addHelper is not a function

          at wrapInterop (node_modules/@babel/helper-module-transforms/lib/index.js:165:45)
          at PluginPass.exit (node_modules/@babel/plugin-transform-modules-commonjs/lib/index.js:174:70)
          at newFn (../node_modules/@babel/traverse/lib/visitors.js:237:21)
          at NodePath._call (../node_modules/@babel/traverse/lib/path/context.js:65:20)
          at NodePath.call (../node_modules/@babel/traverse/lib/path/context.js:40:17)
          at NodePath.visit (../node_modules/@babel/traverse/lib/path/context.js:109:8)
          at TraversalContext.visitQueue (../node_modules/@babel/traverse/lib/context.js:142:16)
          at TraversalContext.visitSingle (../node_modules/@babel/traverse/lib/context.js:102:19)
          at TraversalContext.visit (../node_modules/@babel/traverse/lib/context.js:182:19)
          at Function.traverse.node (../node_modules/@babel/traverse/lib/index.js:106:17)

1 个答案:

答案 0 :(得分:0)

将.babelrc更改为

{
  "presets": ["@babel/preset-env"]
}

注意::.babelrc和babel.config.js是多余的。如果您具有静态配置,则只需.babelrc即可。