mocha-multiple-sessions-lib
v3.0.7
Published
Modern library for running multiple Mocha test sessions in the browser
Downloads
19
Maintainers
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-libVia 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 sessionsetupFn(function): Function to set up tests, receivesloadScriptfunctionoptions(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 withcreateMochaInstance,injectGlobals,loadScriptfunctions
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
