SyntaxError: Cannot use import statement outside a module in Javascript/Jest

Viewed 832

I'm trying to run a test in jest. When I add my import:

import { AnimalFactory } from "../../AnimalFactory";

I get the following error:

 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import axios from "axios/index";

SyntaxError: Cannot use import statement outside a module in Javascript/Jest

 

I believe something about import axios from axios/index is causing an issue. How can I fix this?

I read some suggestions to include "type": "module", so I've added to package.json as an additional line but nothing seems to change

I'm also getting this error:

Cannot find module 'axios/index' from 'AnimalFactory.js'

However, Jest was able to find:
    '../../AnimalFactory.js'

You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

What i've done: installed babel and the plugin:

npm install --save-dev @babel/plugin-transform-modules-commonjs

webpack:

  "babel": {
    "presets": [
      "@babel/preset-env",
      "@babel/preset-react"
    ],
    "plugins": [
      "@babel/plugin-proposal-class-properties",
      "@babel/plugin-transform-modules-commonjs"
    ]
  },
0 Answers
Related