Mocha testing framework that runs test suites within Electron environments for both main and renderer process testing.
npx @tessl/cli install tessl/npm-electron-mocha@13.1.0Electron Mocha is a command-line testing tool that enables running Mocha test suites within Electron environments. It provides the capability to test JavaScript applications in real browser contexts and specifically test Electron desktop applications, supporting both main process (Node.js environment) and renderer process (Chromium browser environment) testing.
npm install -g electron-mochaFor programmatic usage:
const { main } = require("electron-mocha/lib/main");For CLI usage:
electron-mocha [options] [files...]# Run tests in main process (Node.js environment)
electron-mocha test/
# Run tests in renderer process (browser environment with DOM)
electron-mocha --renderer test/
# Run tests with debugging enabled
electron-mocha --renderer --inspect test/
# Run tests in interactive mode (window stays open)
electron-mocha --renderer --interactive test/Electron Mocha is built around several key components:
Complete command-line interface for running Mocha tests within Electron environments, with all standard Mocha options plus Electron-specific configuration.
// CLI executable
electron-mocha [options] [files...]
// Core Electron-specific options
--renderer // Run tests in renderer process
--require-main, --main // Require module in main process [array]
--script // Load module in renderer via script tag [array]
--interactive // Show renderer tests in persistent window
--url, --index // Load custom URL in renderer [string]
--preload // Load module during renderer preload [string]
--window-config // Custom Electron window options [string]
--warnings, -W // Print renderer warnings to console
--show-window // Show the window during tests
--inspect // Enable debugger (default port 5858)Core programmatic interface for integrating Electron Mocha into custom tooling and build systems.
/**
* Main entry point module for programmatic usage
*/
const main: {
/**
* Main entry point function for programmatic usage
* @param argv - Command line arguments array
*/
main(argv?: string[]): void;
};
/**
* Test execution handler from run module
* @param argv - Parsed command line options
*/
async function handler(argv: ElectronMochaOptions): Promise<void>;
// See ElectronMochaOptions in Types section belowEnvironment variables for configuring Electron executable location and runtime behavior.
// Environment variables
ELECTRON_PATH // Path to Electron executable
ELECTRON_RUN_AS_NODE // Used internally for cleanup processesinterface ElectronMochaOptions {
// Electron-specific options
renderer?: boolean;
'require-main'?: string[];
script?: string[];
interactive?: boolean;
url?: string;
preload?: string;
'window-config'?: string;
warnings?: boolean;
'show-window'?: boolean;
inspect?: boolean | number;
// Standard Mocha options also supported
reporter?: string;
grep?: string;
timeout?: number;
recursive?: boolean;
watch?: boolean;
config?: string;
require?: string[];
exit?: boolean;
bail?: boolean;
slow?: number;
color?: boolean;
// All other standard Mocha CLI options supported
}
interface WindowConfig {
width?: number;
height?: number;
webPreferences?: {
enableRemoteModule?: boolean;
contextIsolation?: boolean;
nodeIntegration?: boolean;
preload?: string;
};
// All BrowserWindow options supported
}