Run Cycle Overview
The following is a mid-level outline of Mocha’s “flow of execution” when run in Node.js; the “less important” details have been omitted.
In a browser, test files are loaded by <script>
tags, and calling mocha.run()
begins at step 9 below.
Serial Mode
- User (that’s you) executes
mocha
- Loads options from config files, if present
- Mocha processes any command-line options provided (see section on configuration merging for details)
- If known flags for the
node
executable are found:- Mocha will spawn
node
in a child process, executing itself with these flags - Otherwise, Mocha does not spawn a child process
- Mocha will spawn
- Mocha loads modules specified by
--require
- If a file loaded this way contains known Mocha-specific exports (e.g., root hook plugins), Mocha “registers” these
- If not, Mocha ignores any exports of a
--require
’d module
- Mocha validates any custom reporters or interfaces which were loaded via
--require
or otherwise - Mocha discovers test files; when given no files or directories, it finds files with extensions
.js
,.mjs
or.cjs
in thetest
directory (but not its children), relative to the current working directory - The (default) bdd interface loads the test files in no particular order, which are given an interface-specific
global
context (this is how, e.g.,describe()
ends up as a global in a test file)- When a test file is loaded, Mocha executes all of its suites and finds—but does not execute—any hooks and tests therein.
- Top-level hooks, tests and suites are all made members of an “invisible” root suite; there is only one root suite for the entire process
- Mocha runs global setup fixtures, if any
- Starting with the “root” suite, Mocha executes:
- Any “before all” hooks (for the root suite, this only happens once; see root hook plugins)
- For each test, Mocha executes:
- Any “before each” hooks
- The test (and reports the result)
- Any “after each” hooks
- If the current suite has a child suite, repeat the steps in 10. for each child suite; each child suite inherits any “before each” and “after each” hooks defined in its parent
- Any “after all” hooks (for the root suite, this only happens once; see root hook plugins)
- Mocha prints a final summary/epilog, if applicable
- Mocha runs global teardown fixtures, if any
Parallel Mode
- Repeat steps 1 through 6 from Serial Mode above, skipping reporter validation
- All test files found are put into a queue (they are not loaded by the main process)
- Mocha runs global setup fixtures, if any
- Mocha creates a pool of subprocesses (“workers”)
- Immediately before a worker runs the first test it receives, the worker “bootstraps” itself by:
- Loading all
--require
’d modules - Registering any root hook plugins
- Ignoring global fixtures and custom reporters
- Asserting the built-in or custom interface is valid
- Loading all
- When a worker receives a test file to run, the worker creates a new Mocha instance for the single test file, and:
- The worker repeats step 8 from above
- The worker repeats step 10 from above, with the caveat that the worker does not report test results directly; it holds them in a memory buffer
- When the worker completes the test file, buffered results are returned to the main process, which then gives them to the user-specified reporter (
spec
by default) - The worker makes itself available to the pool; the pool gives the worker another test file to run, if any remain
- Mocha prints a final summary/epilog, if applicable
- Mocha runs global teardown fixtures, if any