Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocha runner does not support require "esm" #3014

Closed
nicojs opened this issue Jul 21, 2021 · 3 comments · Fixed by #3015
Closed

Mocha runner does not support require "esm" #3014

nicojs opened this issue Jul 21, 2021 · 3 comments · Fixed by #3015
Labels
🐛 Bug Something isn't working

Comments

@nicojs
Copy link
Member

nicojs commented Jul 21, 2021

Summary

When using a mocha project with esm, you might run into this error:

20:15:50 (21474) ERROR DryRunExecutor One or more tests resulted in an error:
        SyntaxError: Cannot use import statement outside a module
/home/nicojs/github/mocha-esm-ts-node/.stryker-tmp/sandbox7723690/test/say-hello.spec.ts:2
import assert from 'assert';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at new Script (vm.js:102:7)
    at createScript (vm.js:263:10)
    at Object.runInThisContext (vm.js:311:10)
    at wrapSafe (internal/modules/cjs/loader.js:989:15)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)

The reason for this is that mocha has an exception in place for --require esm:

https://github.com/mochajs/mocha/blob/6eb3c3cda72102144ef2f2dd9b90ddc2d074c9b6/bin/mocha#L88-L94

In order to make this work in Stryker, you would need to rebuild this yourself

Stryker config

{
  "$schema": "./node_modules/@stryker-mutator/core/schema/stryker-schema.json",
  "testRunner": "mocha",
  "mochaOptions": {
    "spec": ["test/*.ts", "test/*.js"],
    "require": ["esm", "ts-node/register"]
  }
}

Test runner config

env TS_NODE_PROJECT=\"tsconfig.json\" mocha -r ts-node/register -r esm \"test/*.{js,ts}\"

Stryker environment

+-- @stryker-mutator/core@5.2.2
+-- @stryker-mutator/mocha-runner@5.2.2

(5.2.2 or earlier)

@nicojs nicojs added the 🐛 Bug Something isn't working label Jul 21, 2021
@nicojs
Copy link
Member Author

nicojs commented Jul 21, 2021

The solution is to change your Stryker config to also require esm the same way as mocha does it:

{
  "$schema": "./node_modules/@stryker-mutator/core/schema/stryker-schema.json",
  "testRunner": "mocha",
+ "testRunnerNodeArgs": ["--require", "esm"],

  "mochaOptions": {
    "spec": ["test/*.ts", "test/*.js"],
-   "require": ["esm", "ts-node/register"]
+   "require": ["ts-node/register"]
  }
}

We should throw an error from the mocha runner, explaining how to fix it.

@nicojs
Copy link
Member Author

nicojs commented Jul 21, 2021

Maybe interesting to know: esm support is dropped from mocha as of v9. See

mochajs/mocha#4671

nicojs added a commit that referenced this issue Jul 22, 2021
Provide a clear error message when users configure `mochaOptions.require` to include `"esm"`. You should provide it as a node argument instead.

```diff
{
  "$schema": "./node_modules/@stryker-mutator/core/schema/stryker-schema.json",
  "testRunner": "mocha",
  "mochaOptions": {
-   "require": ["esm"]
  },
+ "testRunnerNodeArgs": ["--require", "esm"]
}
```

Fixes #3014
@nicojs
Copy link
Member Author

nicojs commented Aug 1, 2021

We released v5.2.3 with the fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant