Skip to content

Commit

Permalink
fix(mocha-runner): clear error when require esm (#3015)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nicojs authored Jul 22, 2021
1 parent 5466cfb commit a835f0b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/mocha-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ Default: `[]`

Set mocha's [`require` option](https://mochajs.org/#-r---require-module-name)

_Note:_ if you want to require [`esm`](https://www.npmjs.com/package/esm), you will need to use `"testRunnerNodeArgs": ["--require", "esm"]` instead. See [#3014](https://github.com/stryker-mutator/stryker-js/issues/3014) for more information.

### `mochaOptions.async-only` [`boolean`]

Default: `false`
Expand Down
5 changes: 5 additions & 0 deletions packages/mocha-runner/src/mocha-test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export class MochaTestRunner implements TestRunner {
this.mochaOptions = this.loader.load(this.options as MochaRunnerWithStrykerOptions);
this.testFileNames = this.mochaAdapter.collectFiles(this.mochaOptions);
if (this.mochaOptions.require) {
if (this.mochaOptions.require.includes('esm')) {
throw new Error(
'Config option "mochaOptions.require" does not support "esm", please use `"testRunnerNodeArgs": ["--require", "esm"]` instead. See https://github.com/stryker-mutator/stryker-js/issues/3014 for more information.'
);
}
this.rootHooks = await this.mochaAdapter.handleRequires(this.mochaOptions.require);
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/mocha-runner/test/unit/mocha-test-runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ describe(MochaTestRunner.name, () => {

expect(sut.rootHooks).eq(expectedRootHooks);
});

it('should reject when requires contains "esm" (see #3014)', async () => {
const requires = ['esm', 'ts-node/require'];
mochaOptionsLoaderMock.load.returns(createMochaOptions({ require: requires }));
await expect(sut.init()).rejectedWith(
'Config option "mochaOptions.require" does not support "esm", please use `"testRunnerNodeArgs": ["--require", "esm"]` instead. See https://github.com/stryker-mutator/stryker-js/issues/3014 for more information.'
);
});
});

describe(MochaTestRunner.prototype.dryRun.name, () => {
Expand Down

0 comments on commit a835f0b

Please sign in to comment.