0

im trying to learn testing but i get this problem. It seem that jest can not import modules. I have tried a lot of configurations but i couldn't solve it

Test suite failed to run

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

/home/alain-maza/Documentos/entrenamiento/vue/04-pokemon/node_modules/axios/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import axios from './lib/axios.js';
                                                                                  ^^^^^^

SyntaxError: Cannot use import statement outside a module

> 1 | import axios from "axios";
    | ^
  2 |
  3 | const pokemonApi = axios.create({
  4 |     baseURL: 'https://pokeapi.co/api/v2/pokemon'

this is my jest.config

 module.exports = {
  preset: '@vue/cli-plugin-unit-jest',
}

this is my babel.config

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ]
}

and my package.json

{
  "name": "04-pokemon",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "test:unit": "vue-cli-service test:unit"
  },
  "dependencies": {
    "axios": "^1.0.0",
    "core-js": "^3.8.3",
    "vue": "^3.2.13"
  },
  "devDependencies": {
    "@babel/core": "^7.19.3",
    "@babel/preset-env": "^7.19.4",
    "@vue/cli-plugin-babel": "~5.0.0",
    "@vue/cli-plugin-unit-jest": "~5.0.0",
    "@vue/cli-service": "~5.0.0",
    "@vue/test-utils": "^2.0.0-0",
    "@vue/vue3-jest": "^27.0.0-alpha.1",
    "babel-jest": "^27.5.1",
    "jest": "^27.0.5"
  }
}

heklpme please! i have trried a lot of things

alain_maza
  • 71
  • 4
  • You need to configure jest so that it will transform axios: https://stackoverflow.com/questions/51127176/unexpected-token-import-error-while-running-jest-tests – Terry Oct 10 '22 at 20:50
  • 1
    thanks I have solved adding transformIgnorePatterns: ["/node_modules/(?!axios)"], in the jest.config – alain_maza Oct 10 '22 at 21:34
  • Glad to hear that trick worked for you :) – Terry Oct 10 '22 at 22:43

0 Answers0