简体   繁体   中英

Jest testing ( using babel ) invalid token import

testing a Firebase Function with Jest , I am in trouble with the import .. in my spec file..

console.log

 import 'jest';
    ^^^^^^

    SyntaxError: Unexpected token import

I am using @babel/core and @babel/preset-env and 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"
  ]
}

And I created a babel.config.js file

babel.config.js

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

what do I miss ? thanks for feedback

UPDATE

I updated my config , as stated in 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"]
    }

But I am still getting an error ... it's not very clear for beginners what should be used actually... too many advices in the doc... I am only using JS for Firebase functions, no React, no Vue...

UPDATE

I updated my .babelrc

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

Now I get another Babel error ...

    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)

Change your .babelrc to

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

NOTE: .babelrc and babel.config.js is redundant. If you have a static configuration then just .babelrc will do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM