A test runner that combines Mocha testing framework with webpack bundling capabilities for modern JavaScript applications
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
The programmatic API provides a fluent interface for configuring and running tests in Node.js applications. It centers around the MochaWebpack class, which offers chainable methods for configuration.
Creates a new MochaWebpack instance with default configuration.
/**
* Creates a new MochaWebpack instance
* @returns {MochaWebpack} New MochaWebpack instance with default options
*/
function createMochaWebpack(): MochaWebpack;Usage Example:
const createMochaWebpack = require('mocha-webpack');
const mochaWebpack = createMochaWebpack();Add test files and included modules to be processed by webpack.
/**
* Add file to run tests against
* @param {string} file - File path or glob pattern
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
addEntry(file: string): MochaWebpack;
/**
* Add file to include in the test bundle
* @param {string} file - Absolute path to module
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
addInclude(file: string): MochaWebpack;Usage Examples:
mochaWebpack
.addEntry('./test/**/*.test.js')
.addEntry('./test/specific.test.js')
.addInclude('/path/to/setup-file.js');Configure the working directory and webpack settings.
/**
* Set the current working directory
* @param {string} cwd - Absolute working directory path
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
cwd(cwd: string): MochaWebpack;
/**
* Set webpack configuration
* @param {object} config - Webpack configuration object
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
webpackConfig(config: object): MochaWebpack;Usage Examples:
mochaWebpack
.cwd(process.cwd())
.webpackConfig({
mode: 'development',
resolve: {
extensions: ['.js', '.ts']
}
});Control test execution behavior including bail, timeouts, and retries.
/**
* Enable or disable bailing on the first test failure
* @param {boolean} bail - Whether to bail on first failure (default: false)
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
bail(bail?: boolean): MochaWebpack;
/**
* Set test timeout in milliseconds
* @param {number} timeout - Timeout in milliseconds (0 disables timeout)
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
timeout(timeout: number): MochaWebpack;
/**
* Set number of times to retry failed tests
* @param {number} count - Number of retries
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
retries(count: number): MochaWebpack;
/**
* Set slowness threshold in milliseconds
* @param {number} threshold - Slow test threshold in milliseconds
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
slow(threshold: number): MochaWebpack;Configure test interface, reporters, and output formatting.
/**
* Set test UI interface
* @param {string} ui - Interface type: 'bdd', 'tdd', 'exports', 'qunit'
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
ui(ui: string): MochaWebpack;
/**
* Set test reporter and options
* @param {string|Function} reporter - Reporter name or constructor
* @param {object} reporterOptions - Reporter-specific options
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
reporter(reporter: string | Function, reporterOptions: object): MochaWebpack;
/**
* Force color output
* @param {boolean} colors - Whether to use colors
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
useColors(colors: boolean): MochaWebpack;
/**
* Use inline diffs instead of +/-
* @param {boolean} inlineDiffs - Whether to use inline diffs
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
useInlineDiffs(inlineDiffs: boolean): MochaWebpack;
/**
* Suppress informational messages
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
quiet(): MochaWebpack;Filter which tests to run based on patterns or strings.
/**
* Only run tests containing the specified string
* @param {string} str - String to search for in test names
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
fgrep(str: string): MochaWebpack;
/**
* Only run tests matching the specified pattern
* @param {string|RegExp} pattern - Regular expression or string pattern
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
grep(pattern: string | RegExp): MochaWebpack;
/**
* Invert grep and fgrep matches
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
invert(): MochaWebpack;Additional configuration options for specialized testing scenarios.
/**
* Ignore global variable leaks
* @param {boolean} ignore - Whether to ignore leaks
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
ignoreLeaks(ignore: boolean): MochaWebpack;
/**
* Display full stack traces on failing tests
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
fullStackTrace(): MochaWebpack;
/**
* Force all tests to be async (accepting a callback)
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
asyncOnly(): MochaWebpack;
/**
* Delay root suite execution
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
delay(): MochaWebpack;
/**
* Force interactive mode
* @param {boolean} interactive - Whether to use interactive mode
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
interactive(interactive: boolean): MochaWebpack;
/**
* Enable growl notification support
* @returns {MochaWebpack} MochaWebpack instance for chaining
*/
growl(): MochaWebpack;Execute tests either once or in watch mode.
/**
* Run tests once
* @returns {Promise<number>} Promise resolving to number of failed tests
* @throws {Error} Build errors during webpack compilation
*/
run(): Promise<number>;
/**
* Run tests and watch for file changes
* @returns {Promise<void>} Promise that resolves when watch mode ends
* @description Automatically reruns affected tests when files change
*/
watch(): Promise<void>;Usage Examples:
// Run tests once
mochaWebpack
.addEntry('./test/**/*.test.js')
.webpackConfig(webpackConfig)
.run()
.then((failures) => {
console.log(\`Tests completed with \${failures} failures\`);
process.exit(failures);
})
.catch((err) => {
console.error('Build failed:', err);
process.exit(1);
});
// Watch mode
mochaWebpack
.addEntry('./test/**/*.test.js')
.webpackConfig(webpackConfig)
.watch()
.catch((err) => {
console.error('Watch mode failed:', err);
process.exit(1);
});/**
* Current test entries (files/globs to test)
*/
entries: string[];
/**
* Files to include in the test bundle
*/
includes: string[];
/**
* Current configuration options
*/
options: MochaWebpackOptions;const createMochaWebpack = require('mocha-webpack');
const webpackConfig = require('./webpack.config.js');
const mochaWebpack = createMochaWebpack();
mochaWebpack
// Add test files
.addEntry('./test/**/*.test.js')
.addInclude('./test/setup.js')
// Configure webpack
.webpackConfig(webpackConfig)
// Configure mocha
.ui('bdd')
.reporter('spec')
.timeout(5000)
.bail(true)
// Configure filtering
.grep('integration')
// Configure output
.useColors(true)
.quiet()
// Run tests
.run()
.then((failures) => {
process.exit(failures);
})
.catch((err) => {
console.error(err);
process.exit(1);
});