0

I am unable to load an .mjs file and run tests with mocha.

I have tried mocha v.7.2.0 with flag --experimental-modules and v.10.0.0 without flag.

I am using node v.14.15.4.

The import statement that I have in my code to test is:

import * as XLSX from 'xlsx/xlsx.mjs';

NB: this import statement works 100% fine while building my app with Webpack.

The error I get when I try to run unit tests on this code with Mocha is:

(node:5452) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use node --trace-warnings ... to show where the warning was created)
C:\Users......webapp\node_modules\xlsx\xlsx.mjs:81

export { set_cptable };
^^^^^^
SyntaxError: Unexpected token 'export' at wrapSafe (internal/modules/cjs/loader.js:979:16)

My mocha invocation is a vanilla mocha \"src/**/*.spec.js\", which I call through an npm task; as I mentioned before, flag --experimental-modules does not help.

My .babelrc, which (I suspect) is used to transpile my code, is the following:

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-transform-runtime",
    "react-hot-loader/babel",
    ["module-resolver", {
      "alias": {
        "@src": "./src"
      }
    }]
  ],
  "env": {
    "coverage": {
      "plugins": [
        "istanbul"
      ]
    }
  }
}

What can I do to solve this problem?

Marco Faustinelli
  • 3,734
  • 5
  • 30
  • 49

0 Answers0