Mocha doesn't take in account esm package

Hello, since today I was using run configuration of npm script which was doing this: 

"test": "mocha -r tsconfig-paths/register -r ts-node/register -r chai/register-expect -r esm src/**/*.spec.ts libs/tracking-changes/__tests__/**/*.spec.ts libs/tracking-changes/src/**/*.spec.ts",

I decided to move my npm script to Mocha run configuration. So I made a file called .mocharc.js and put there:

module.exports = {
diff: true,
fullTrace: true,
require: ["esm", "tsconfig-paths/register", "ts-node/register", "chai/register-expect"],
spec: ["src/**/*.spec.ts", "libs/tracking-changes/__tests__/**/*.spec.ts", "libs/tracking-changes/src/**/*.spec.ts"],
}

then I created a Mocha configuration with UI bdd and with option set to "File pattern". (there is no correct option for empty), so this one has placeholder with using default mocha options. Mocha package is used from project and Node interpreter from system. (exactly like NPM does it)

 

When I run the test via Jetbrans I'm getting error

/processors/NegoWordProcessor.spec.ts:1
(function (exports, require, module, __filename, __dirname) { import DeltaBridge from "../../../../../../libs/tracking-changes/src/@formats/Delta/DeltaBridge";
^^^^^^

SyntaxError: Cannot use import statement outside a module

 

The same error occurs when `esm` package is not present. So I tried to put custom options to Extra mocha options like: "-r esm -r etc..." and the same error is still here.

 

This error happens only inside Jetbrains, when I run the npm (mentioned above) via terminal or NPM script run configuration then it works fine.

 

I'm using latest version of IntelliJ IDEA Ultimate, all mentioned packages and last version of Ubuntu.

5 comments
Comment actions Permalink

works fine for me, both when using .mocharc.js and specifying --require directly in run configuration, in Extra Mocha Options: field.

Please could you share a sample project the issue can be reproduced in? I'm not asking for your proprietary files, can just be a pair of dummy spec files, but the configuration/project structure should be the same

0
Comment actions Permalink

Hello Elena, I was trying to reproduce it on a sample project but failed. Then I copied tsconfig.json and the issue started happening there too:

Try create `tsconfig.json` in your root directory with this content:

{
"compilerOptions": {
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"target": "es2020",
"lib": ["dom", "es2020"],
"importHelpers": true,
"baseUrl": ".",
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"inlineSources": true,
"sourceRoot": "/"
},
"exclude": [
"node_modules"
]
}

If you still need, I can make the project sample. Do you have any server where I should upload it?

0
Comment actions Permalink

Anyway I made a repo on Github: https://github.com/sionzeecz/mocha-issue

Quick steps to test it:

1. Clone the project

2. Run `npm install` to install node_modules

3. a) Run `npm test` which should return ` 1 passing `

3. b) Run configuration `Mocha - Test` where is expected the same output as at point a)

If Mocha - Test is not there, then create a new configuration from Mocha template and set toggle to File patterns.

0
Comment actions Permalink

Thanks, reproduced.

WebStorm runs mocha using node node_modules\mocha\bin\_mocha command (required for debugger to work); but, unlike node_modules\mocha\bin\mocha that wraps mocha cli providing special handling for esm, _mocha runs lib/cli/cli.js directly, so -r esm is not moved to Node.js flags

 

To work out the issue, specify the flag explicitly in your Mocha run configuration, Node options: field:

1
Comment actions Permalink

Thank you very much Elena! You are a genius! :)

1

Please sign in to leave a comment.