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 [regex]
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

Installation:
npm install -g mocha

Usage:
mocha [options] [files/directories]

PARAMETERS

--require <file> / -r <file>
    Specifies a module to be required before tests run, useful for transpilation or configuration.

--timeout <ms> / -t <ms>
    Sets the timeout in milliseconds for tests; tests exceeding this time will fail. Default is 2000ms.

--grep <pattern> / -g <pattern>
    Runs only tests whose titles match the given regular expression pattern.

--fgrep <string> / -f <string>
    Runs only tests whose titles contain the given string.

--watch / -w
    Watches test files and automatically re-runs tests on changes.

--reporter <name>
    Specifies the test reporter to use (e.g., spec, dot, nyan, json, list).

--bail
    Aborts testing after the first test failure.

--recursive
    Searches for test files recursively within specified directories.

--exit
    Forces Mocha to exit after all tests complete, useful when tests leave event loops open.

--full-trace
    Displays full stack traces for test failures.

DESCRIPTION

Mocha is a feature-rich JavaScript test framework designed to run tests in both Node.js and the browser. It provides a clean, flexible, and simple API for writing tests, making it a popular choice for developers. Key features include support for asynchronous testing, various test lifecycle hooks (before, after, beforeEach, afterEach), test retries, and a wide array of built-in and custom reporters for visualizing test results. Unlike many core Linux commands, Mocha is not a native system utility but rather a program executed via the Node.js runtime environment. It is typically installed and managed using the Node Package Manager (npm) within a development project or globally on a system to allow command-line invocation. Its flexibility in handling different testing styles (BDD, TDD) and its extensibility through plugins make it a powerful tool for ensuring code quality in JavaScript applications.

CAVEATS

Mocha is a JavaScript framework and not a standalone Linux utility. It requires Node.js and npm to be installed on your system. It functions by executing JavaScript test files within the Node.js runtime. Therefore, its availability and behavior depend on the Node.js environment, not the underlying Linux distribution's core commands.

TEST REPORTERS

Mocha supports a variety of built-in test reporters that determine how test results are displayed in the console. Common reporters include spec (default, human-readable hierarchy), dot (minimalist dots for each test), nyan (a whimsical cat animation), json (machine-readable output), and list (a detailed list of test titles). Users can also create custom reporters or use third-party ones to tailor the output to their specific needs, enhancing debuggability and integration with CI/CD systems.

HISTORY

Mocha was created by TJ Holowaychuk and first released in November 2011. It quickly gained popularity as a robust and flexible testing framework for JavaScript, particularly known for its excellent handling of asynchronous code and its extensibility. Since its inception, it has been actively maintained and developed by a dedicated community, adapting to new JavaScript features and evolving testing paradigms. Its design philosophy emphasizes simplicity, speed, and comprehensive reporting.

SEE ALSO

npm(1), node(1), jest(1), yarn(1)

Copied to clipboard