Introduction
Mocha.js is a free JavaScript test framework that runs in the browser and on Node.js. It has a very simple interface for testing both synchronous and asynchronous code.
Mocha.js executes tests in a sequential/serial order to provide flexible and accurate reporting, while also mapping uncaught exceptions to their respective test cases. Mocha.js provides functions that run in a predetermined order and log the results in the terminal window. It also cleans up the state of the software under test so that test cases can execute independently.
In this blog we will discuss the generic flow of tasks in serial mode and parallel mode. These are the two modes of workflow supported in Mocha.js.
Serial Mode
- Mocha is run by the user.
- Mocha processes any command-line parameters provided and loads options from config files if they are present.
-
If the node executable has any known flags:
- Mocha will create a child process and execute it with these flags.
- Mocha does not launch a child process if this is not the case.
-
--require tells Mocha which modules to load.
- Mocha "registers" known Mocha-specific exports (e.g., root hook plugins) in a file loaded this way.
- Otherwise, any exports of a –require'd module are ignored by Mocha.
- Any custom reporters or interfaces loaded using –require or otherwise are validated by Mocha.
- When no files or directories are specified, Mocha looks for files with the extensions .js, .mjs, or .cjs in the test directory (but not its descendants), relative to the current working directory.
-
The (default) bdd interface imports the test files in any order and gives them an interface-specific global context (this is how, for example, describe() becomes a global in a test file).
- When you load a test file, Mocha runs all of its suites and looks for–but does not run–any hooks and tests.
- All top-level hooks, tests, and suites are made members of an "invisible" root suite; the entire process has only one root suite.
- If there are any global setup fixtures, Mocha will run them.
- Mocha begins by executing the "root" suite:
- Any hooks that go "before all" (for the root suite, this only happens once;
-
Mocha runs the following tests for each one:
- Any hooks that go "before each"
- The test (and reports the result)
- Any hooks that go "after each"
- If the current suite has a child suite, repeat steps 10 through 12 for each child suite; each child suite inherits any "before each" and "after each" hooks defined in its parent suite.
- Any "after all" hooks. (for the root suite, this only happens once; see root hook plugins)
- If necessary, Mocha prints a final summary/epilog.
- If there are any global teardown fixtures, Mocha will run them.





