Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mocha-esm-loader

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mocha-esm-loader

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

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Weekly downloads
 
Created
Source

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 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

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

Keywords

FAQs

Package last updated on 03 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc