LinuxCommandLibrary

mocha

Run JavaScript unit tests

TLDR

Run tests with default configuration or as configured in mocha.opts

$ mocha
copy

Run tests contained at a specific location
$ mocha [directory/with/tests]
copy

Run tests that match a specific grep pattern
$ mocha --grep [regular_expression]
copy

Run tests on changes to JavaScript files in the current directory and once initially
$ mocha --watch
copy

Run tests with a specific reporter
$ mocha --reporter [reporter]
copy

SYNOPSIS

mocha [options] [files]

PARAMETERS

-A, --async-only
    Forces all tests to use a callback (async) or return a Promise.

-b, --bail
    Abort after the first test failure.

--check-leaks
    Check for global variable leaks.

-c, --colors
    Force enabling of colors.

--compilers :,...
    Use the given module(s) to compile files.

-C, --no-colors
    Force disabling of colors.

-d, --debug
    Enable node's debugger, synonym for node --inspect.

--debug-brk
    Enable node's debugger breaking on start.

-g, --grep
    Only run tests matching the given pattern.

-f, --fgrep
    Only run tests containing the given string.

-G, --growl
    Enable Growl notifications.

-h, --help
    Output usage information.

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

-m, --timeout
    Specify test timeout threshold in milliseconds [2000].

-n, --no-deprecation
    Silence deprecation warnings.

-N, --no-timeouts
    Disable timeouts.

-O, --reporter-options
    Reporter-specific options.

-p, --parallel
    Run tests in parallel.

--file
    Require file

-R, --reporter
    Specify reporter to use [spec].

-r, --require ,...
    Require module(s).

-s, --slow
    Specify "slow" test threshold in milliseconds [75].

-S, --sort
    Sort test files.

-t, --timeout
    Specify test timeout threshold in milliseconds [2000].

-u, --ui
    Specify user-interface (bdd|tdd|exports).

-V, --version
    Output version information.

-w, --watch
    Watch files for changes.

DESCRIPTION

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser. It provides a clean and simple interface for writing and running tests. Mocha supports various assertion libraries like Chai, Should.js, and Expect.js, allowing developers to choose their preferred style. It supports asynchronous testing, allowing testing of code that uses callbacks, promises, or async/await. Key features include test coverage reporting, flexible reporters (e.g., spec, dot, json), and support for different testing methodologies like Behavior-Driven Development (BDD), Test-Driven Development (TDD), and Exports interface. Mocha makes it easy to organize tests into suites and cases, and provides hooks for setup and teardown. Mocha is extensible via plugins. It can be used for unit tests, integration tests, and end-to-end tests.

CAVEATS

Mocha relies on Node.js or a browser environment to execute tests. Its behavior and capabilities can be influenced by the Node.js version or browser features.
Test execution order may vary unless explicitly controlled. Parallel test execution (using -p) can introduce complexities related to shared state and resource contention, potentially affecting test reliability.

EXAMPLES

To run tests in the 'test' directory:
mocha test/
To run a specific test file:
mocha test/my_test.js
To run tests with a specific reporter (e.g., 'nyan'):
mocha -R nyan test/

HISTORY

Mocha was created by TJ Holowaychuk in 2011. Initially designed as a simple test runner, it gained popularity for its flexibility and extensibility.
Over the years, Mocha has evolved significantly, adding support for asynchronous testing, reporters, and different UI styles. It has become a standard choice for JavaScript testing in both Node.js and browser environments.

SEE ALSO

chai(1), node(1)

Copied to clipboard