CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-mocha-webpack

A test runner that combines Mocha testing framework with webpack bundling capabilities for modern JavaScript applications

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

cli.mddocs/

Command Line Interface

The mocha-webpack CLI provides a command-line interface for running tests with webpack precompilation. It maintains compatibility with mocha's CLI options while adding webpack-specific functionality.

Capabilities

Basic Command Structure

mocha-webpack [options] [files...]

If no files are specified, defaults to ./test.

Basic Options

Core functionality for test execution and configuration.

# Show help information
--help, -h, -?

# Show version number
--version

# Specify test UI interface (default: bdd)
--ui, -u <interface>
# Valid interfaces: bdd, tdd, exports, qunit

# Watch files for changes and rerun tests
--watch, -w

# Set number of times to retry failed test cases
--retries <count>

# Path to webpack configuration file (default: webpack.config.js)
--webpack-config <path>

# Environment passed to webpack config when it's a function
--webpack-env <env>

# Path to mocha-webpack options file
--opts <path>

Output Options

Control test output formatting and verbosity.

# Force enabling of colors
--colors, -c

# Suppress informational messages
--quiet, -q

# Force interactive mode (default: enabled in terminal)
--interactive

# Enable growl notification support
--growl, -G

# Specify the reporter to use (default: spec)
--reporter, -R <name>

# Reporter-specific options (format: k=v,k2=v2,...)
--reporter-options, -O <options>

Advanced Options

Detailed test configuration and filtering options.

# Force all tests to take a callback (async) or return a promise
--async-only, -A

# Bail after first test failure
--bail, -b

# Only test files matching pattern (for directory entries)
--glob <pattern>

# Only run tests matching pattern
--grep, -g <pattern>

# Only run tests containing string
--fgrep, -f <string>

# Invert --grep and --fgrep matches
--invert, -i

# Require the given module before running tests
--require, -r <module>
# Can be used multiple times

# Include the given module in test bundle
--include <module>
# Can be used multiple times

# "Slow" test threshold in milliseconds (default: 75ms)
--slow, -s <ms>

# Set test-case timeout in milliseconds (default: 2000ms)
--timeout, -t <ms>

# Check for global variable leaks
--check-leaks

# Display the full stack trace
--full-trace

# Display actual/expected differences inline within each string
--inline-diffs

# Require a clean shutdown of the event loop
--exit

# Wait for async suite definition
--delay

# Include subdirectories when using directory as test entry
--recursive

Usage Examples

Basic Usage

# Run a single test file
mocha-webpack simple.test.js

# Run all tests matching a glob pattern
mocha-webpack "test/**/*.js"

# Run tests in a directory with specific pattern
mocha-webpack --glob "*.test.js" test

# Include subdirectories
mocha-webpack --recursive --glob "*.test.js" test

Watch Mode

# Watch for changes and rerun tests
mocha-webpack --watch test

# Watch with specific webpack config
mocha-webpack --watch --webpack-config webpack.test.js test

# Watch with custom reporter
mocha-webpack --watch --reporter json test

Filtering Tests

# Run only tests matching a pattern
mocha-webpack --grep "user.*login" test

# Run only tests containing specific string
mocha-webpack --fgrep "authentication" test

# Invert the filter (run tests NOT matching pattern)
mocha-webpack --grep "integration" --invert test

Configuration

# Use custom webpack config
mocha-webpack --webpack-config webpack.test.js test

# Pass environment to webpack config function
mocha-webpack --webpack-config webpack.config.js --webpack-env test test

# Use options file
mocha-webpack --opts test/mocha-webpack.opts

# Require setup modules
mocha-webpack --require babel-register --require ./test/setup.js test

# Include modules in test bundle
mocha-webpack --include ./src/polyfills.js test

Output Control

# Use specific reporter
mocha-webpack --reporter json test

# Reporter with options
mocha-webpack --reporter xunit --reporter-options output=results.xml test

# Quiet mode (suppress info messages)
mocha-webpack --quiet test

# Force colors even when not in TTY
mocha-webpack --colors test

# Enable growl notifications
mocha-webpack --growl test

Advanced Configuration

# Set timeout for slow tests
mocha-webpack --timeout 10000 test

# Set slow test threshold
mocha-webpack --slow 200 test

# Retry failed tests
mocha-webpack --retries 3 test

# Bail on first failure
mocha-webpack --bail test

# Force async tests only
mocha-webpack --async-only test

# Use TDD interface instead of BDD
mocha-webpack --ui tdd test

# Enable full stack traces
mocha-webpack --full-trace test

# Check for global leaks
mocha-webpack --check-leaks test

Configuration Files

Webpack Configuration

By default, mocha-webpack looks for webpack.config.js in the current directory. You can specify a different file:

mocha-webpack --webpack-config custom-webpack.config.js test

If your webpack config is a function, you can pass an environment:

mocha-webpack --webpack-config webpack.config.js --webpack-env test test

Options File

Use an options file to avoid repeating command-line arguments:

mocha-webpack --opts test/mocha-webpack.opts

Example options file content:

--webpack-config webpack.test.js
--require babel-register
--recursive
--timeout 5000
--reporter spec
test

Exit Codes

  • 0: All tests passed
  • 1: Test failures occurred or build error
  • Number: Number of failed tests (when using --exit)

Environment Variables

Mocha-webpack respects standard environment variables:

  • NODE_ENV: Node.js environment
  • FORCE_COLOR: Force color output
  • NO_COLOR: Disable color output

Glob Patterns

When using glob patterns, remember to quote them to prevent shell expansion:

# Correct - quoted pattern
mocha-webpack "test/**/*.test.js"

# Incorrect - shell will expand the pattern
mocha-webpack test/**/*.test.js

Integration Examples

With npm scripts

{
  "scripts": {
    "test": "mocha-webpack --webpack-config webpack.test.js test",
    "test:watch": "npm run test -- --watch",
    "test:coverage": "nyc npm run test"
  }
}

With CI/CD

# Basic CI test command
mocha-webpack --reporter json --bail test > test-results.json

# With coverage and JUnit output
nyc --reporter lcov mocha-webpack --reporter xunit --reporter-options output=test-results.xml test

docs

cli.md

index.md

programmatic-api.md

tile.json