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

mocha-multiple-sessions-lib

v3.0.7

Published

Modern library for running multiple Mocha test sessions in the browser

Downloads

19

Readme

Mocha Multiple Sessions Library

A modern library for running multiple Mocha test sessions in the browser with support for dynamic test loading and session management.

Features

  • 🚀 Run multiple independent Mocha test sessions
  • 📦 Dynamic test file loading
  • 🎯 Session-based test organization
  • 🔧 Configurable test environment
  • 📊 Built-in support for detailed JSON reporting
  • 🌐 Works with CDN or bundled installations
  • 📱 Browser-focused design

Installation

Via NPM

npm install mocha-multiple-sessions-lib

Via CDN (Browser)

<!-- UMD Bundle -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/iife/mocha-multiple-sessions.min.js"></script>

<!-- ESM Module -->
<script type="module">
import { testSession, runSession, runAllSessions } from 'https://cdn.jsdelivr.net/npm/[email protected]/build/esm-bundled/mocha-multiple-sessions.mjs';
</script>

Basic Usage

1. Include Required Dependencies

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/9.1.3/mocha.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/9.1.3/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.3.4/chai.min.js"></script>

2. Define Test Sessions

import { testSession, runSession, runAllSessions } from 'mocha-multiple-sessions-lib';

// Simple inline test session
testSession('Math Tests', () => {
  describe('Basic Math', () => {
    it('adds correctly', () => {
      expect(1 + 1).to.equal(2);
    });
  });
});

// Session with external test files
testSession('Calculator Tests', async (loadScript) => {
  await loadScript('./tests/calculator-test.js');
  await loadScript('./tests/advanced-math.js');
});

3. Run Sessions

// Run a specific session
runSession('Math Tests').then(result => {
  console.log(`Passes: ${result.stats.passes}`);
  console.log(`Failures: ${result.stats.failures}`);
});

// Run all sessions
runAllSessions().then(results => {
  results.forEach(({ label, result }) => {
    console.log(`${label}: ${result.stats.passes} passes, ${result.stats.failures} failures`);
  });
});

Advanced Configuration

Custom Mocha Instance Creation

import { testSessionSetup } from 'mocha-multiple-sessions-lib';

testSessionSetup({
  createMochaInstance: (sessionLabel) => {
    return new Mocha({
      ui: 'bdd',
      reporter: Mocha.reporters.JSON,
      timeout: 5000
    });
  }
});

Using with DetailedJsonReporter

testSessionSetup({
  createMochaInstance: (sessionLabel) => {
    const resultKey = `results_${sessionLabel.replace(/\s+/g, '_')}`;
    
    return new Mocha({
      ui: 'bdd',
      reporter: window.MochaDetailedReporter?.DetailedJsonReporter || Mocha.reporters.HTML,
      reporterOptions: {
        outputToWindow: 'true',
        windowVariableName: resultKey
      }
    });
  }
});

API Reference

Core Functions

testSession(label, setupFn, options?)

Define a new test session.

  • label (string): Unique label for the session
  • setupFn (function): Function to set up tests, receives loadScript function
  • options (object, optional): Session configuration options

runSession(label)

Run a specific test session by label.

  • label (string): Label of the session to run
  • Returns: Promise resolving to test results

runAllSessions()

Run all defined sessions that haven't been executed yet.

  • Returns: Promise resolving to array of session results

testSessionSetup(config)

Configure the test environment.

  • config (object): Configuration with createMochaInstance, injectGlobals, loadScript functions

Utility Functions

getAllSessions()

Get all defined sessions.

  • Returns: Array of session objects

getSession(label)

Get a specific session by label.

  • label (string): Session label
  • Returns: Session object or null

Examples

Complete HTML Example

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/9.1.3/mocha.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/9.1.3/mocha.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.3.4/chai.min.js"></script>
</head>
<body>
  <div id="mocha-report"></div>
  
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/iife/mocha-multiple-sessions.min.js"></script>
  <script>
    const { testSession, runAllSessions } = MochaMultipleSessions;
    
    testSession('Quick Tests', () => {
      describe('Example', () => {
        it('works', () => {
          expect(true).to.be.true;
        });
      });
    });
    
    runAllSessions().then(results => {
      console.log('Tests completed:', results);
    });
  </script>
</body>
</html>

Browser Compatibility

  • Modern browsers with ES2015+ support
  • Requires Mocha and Chai to be loaded globally
  • Works with module bundlers (Webpack, Rollup, Vite)

Contributing

Contributions are welcome! Please ensure tests pass and follow the existing code style.

License

ISC License