# mocha-esm-loader

> This module helps running Mocha programmatically when writing tests in an ES Module project.

Latest version **0.0.1** (published 2023-06-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install mocha-esm-loader
pnpm add mocha-esm-loader
yarn add mocha-esm-loader
bun add mocha-esm-loader
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2023-06-03 |
| First published | 2023-06-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 4.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | James Mortensen |
| Maintainers | jmort253 |
| Keywords | mocha, esm, programmatically, mocha-multi |

## Links

- npm: https://www.npmjs.com/package/mocha-esm-loader
- Repository: https://github.com/jamesmortensen/mocha-esm-loader
- Homepage: https://github.com/jamesmortensen/mocha-esm-loader/blob/master/README.md
- Issues: https://github.com/jamesmortensen/mocha-esm-loader/issues
- npm.io page: https://npm.io/package/mocha-esm-loader

## Dependencies (1)

- [glob](https://npm.io/package/glob.md) ^10.2.6

## Recent versions

- 0.0.1 (latest) — 2023-06-03

## README

# mocha-esm-loader

This module helps running Mocha programmatically when writing tests in an ES Module project.


## Installation

```
$ npm i mocha-esm-loader
```

## Usage

Load all *.spec.[c|m]js or *.test.[c|m]js files:
```
const mochaEsmLoader = new MochaEsmLoader(mocha, logger);
await mochaEsmLoader.import();

const suiteRun = mocha.run();
```

We also can override the default file match pattern:

```
const mochaEsmLoader = new MochaEsmLoader(mocha, logger);
await mochaEsmLoader.import(['./test/my-esm-test.js']);

const suiteRun = mocha.run();
```


## Example

This example uses a launcher.js ES module script to execute tests written in ESM. It uses mocha-multi to load both the spec reporter as well as a custom reporter. It also uses a fork of [mocha-multi](https://github.com/jamesmortensen/mocha-multi) to load multiple reporters. Mocha cannot load more than one reporter at a time.

For this example, install the following modules:
```
$ npm i mocha-esm-loader mocha
$ npm i github:jamesmortensen/mocha-multi
```

Execute the below script with:

```
$ node launcher.js
```

**launcher.js**
```javascript
import Mocha from 'mocha';

// library that loads ESM-spec files.
import MochaEsmLoader from 'mocha-esm-loader';

// These lines make "require" available
// see https://www.kindacode.com/article/node-js-how-to-use-import-and-require-in-the-same-file/
import { createRequire } from 'module';
global.require = createRequire(import.meta.url);

const Reporter = (await import('./custom-esm-reporter')).default;

const mocha = new Mocha({
    reporter: "mocha-multi",
    reporterOptions: {
            spec: "-",
            "custom-esm-reporter": {
                "constructorFn": Reporter,
                "stdout": "/tmp/mocha-reporter.out",
                "options": {
                    "option1": "value1"
                }
            }
    }
});

const mochaEsmLoader = new MochaEsmLoader(mocha, logger);
await mochaEsmLoader.import();

const suiteRun = mocha.run();

process.on('exit', (code) => {
    process.exit(suiteRun.stats.failures);
});
```

## License

Copyright (c) James Mortensen, 2023 MIT License

---
_Source: https://npm.io/package/mocha-esm-loader · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
