代码之家  ›  专栏  ›  技术社区  ›  user762579

Jest测试(使用babel)无效的令牌导入

  •  1
  • user762579  · 技术社区  · 7 年前

    使用Jest测试Firebase函数时,我在导入方面遇到了麻烦。。在我的规范文件中。。

    console.log

     import 'jest';
        ^^^^^^
    
        SyntaxError: Unexpected token import
    

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

    "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"
          ]
        ]
      };
    

    我错过了什么? 谢谢你的反馈

    更新

    我用Babel文档更新了我的配置,如Jest中所述…(第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"?
    

        "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"]
        ]
        };
    

    B.法律改革委员会

        {
          "presets": ["env"]
        }
    

    但我仍然得到一个错误。。。对于初学者来说,实际应该使用什么还不是很清楚。。。文件中的建议太多。。。 我只在Firebase函数中使用JS,没有React,没有Vue。。。

    更新

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

    现在我又犯了一个巴别塔错误。。。

        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 回复  |  直到 7 年前
        1
  •  0
  •   Revanth M    7 年前

    将您的.LRC更改为

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

    注: