npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

rollup-plugin-mocha

v0.0.1

Published

A Rollup plugin wrapper for Mocha.

Downloads

8

Readme

rollup-plugin-mocha

npm Node.js CI

A Rollup plugin wrapper for Mocha.

Install

npm install --save-dev rollup-plugin-mocha

Install Mocha if you haven't already:

npm install --save-dev mocha

Usage

Use the module.

// ES6
import mocha from 'rollup-plugin-mocha';

// CommonJS
const mocha = require('rollup-plugin-mocha').default;

Example (rollup-test.config.js):

// rollup-test.config.js
import mocha from 'rollup-plugin-mocha';

export default {
  input: 'test/index.spec.js',
  output: {
    file: 'tmp/test.cjs',
    format: 'cjs'
  },
  plugins: [mocha()]
};

Use the plugin with esbuild Rollup plugin and multi-entry:

// rollup-test.config.js
import multi from '@rollup/plugin-multi-entry';
import esbuild from 'rollup-plugin-esbuild';
import mocha from 'rollup-plugin-mocha';

export default {
  // typescript multiple spec files
  // as input under the `test/` directory
  input: 'test/**/*.spec.ts',
  output: {
    dir: 'tmp',
    format: 'cjs',
    entryFileNames: '[name].cjs'
  },
  plugins: [mocha(), multi(), esbuild()]
};

Note: This does not include any type checking or linting. You can check other plugins for this such as @rollup/plugin-typescript and @rollup/plugin-eslint.

With these examples, you can add a script using Rollup to your package.json:

{
  "test": "rollup -c rollup-test.config.js"
}

Run the tests:

npm test

# rollup watch mode
npm test -- -w

Plugin Options

You can pass an options object or a setup callback to the plugin.

Options Object

All properties are optional.

mocha({
  /**
   * Set to `true` to not delete cache from `require.cache`.
   */
  cache: false,

  /**
   * By default, generated output files are not deleted
   * after running tests. Set to `true` to delete them.
   */
  clear: false,

  /**
   * Create Mocha instance.
   */
  instance: () => new Mocha(),

  /**
   * Filter files that will be added to the Mocha instance.
   */
  filterFiles: (files: string[]) => files,

  /**
   * This callback is fired when `mocha.run()` is called.
   */
  runner: async (runner: Mocha.Runner, mocha: Mocha): Promise<void> => {},

  /**
   * This callback is fired once everything is settled.
   */
  finally: async (mocha: Mocha): Promise<void> => {}
});

If you want to have full control over how the plugin runs the test, you can use a setup callback.

Setup Callback

By default, this is how it would look like:

// api instance and output bundle are passed
mocha(async (api: RollupMochaApi, bundle: OutputBundle) => {
  const mocha = api.instance(); // or create Mocha instance yourself
  const files = api.getFiles(bundle); // or get the files yourself
  try {
    await api
      .noCache(mocha) // sets up cache delete from `require.cache`
      .addFiles(files, mocha) // or add files yourself
      .run(mocha.run()); // or handle `mocha.run()` yourself
  } finally {
    await api.removeFiles(files); // or remove files yourself
  }
});

An API instance is passed to the setup callback.

API Instance

The API instance contains the wrapper logic that runs the plugin by default.

interface RollupMochaApi {
  /**
   * Output directory path.
   */
  readonly dir: string;

  /**
   * Create a Mocha instance (without options).
   */
  instance(): Mocha;

  /**
   * Clear cache from `require.cache` to enable rerunning tests in watch mode.
   * @see https://github.com/mochajs/mocha/issues/995#issuecomment-365441585
   */
  noCache(mocha: Mocha): this;

  /**
   * Get the generated file paths from the output bundle.
   */
  getFiles(bundle: OutputBundle): string[];

  /**
   * Add files to the Mocha instance.
   */
  addFiles(files: string[], mocha: Mocha): this;

  /**
   * Delete files from system. File paths should only be within
   * the output directory (`dir`). Otherwise, an error is thrown.
   */
  removeFiles(files: string[], force?: boolean): Promise<void>;

  /**
   * Adds an `end` event listener to the runner.
   * An error is thrown if there are `failures`.
   */
  run(runner: Mocha.Runner): Promise<void>;
}

To sum up:

// log output directory
console.log(api.dir);

// create instance (without options)
const mocha = api.instance();

// apply no cache
api.noCache(mocha);

// get files from bundle
const files = api.getFiles(bundle);

// add files to Mocha instance
api.addFiles(files, mocha);

// add `end` event listener, throws an error if tests have `failures`
await api.run(mocha.run());

// delete files
await api.removeFiles(files, force);

Disclaimer

The author of this package does not have sufficient experience working with Mocha. This explains why the API of this plugin is very minimal when it comes to working with Mocha as it mostly does not attempt to directly tinker with the Mocha instance or load any options. Instead, consumers can set it up themselves.

Feel free to contribute if there are issues or any features that would greatly improve the functionality and experience of this plugin. It would mean a lot, thank you!

License

Licensed under the MIT License.