# Behavior traces — mochajs/mocha

> Canonical page: https://codevetter.com/unpack/mocha/behavior-traces

# Behavior traces — mochajs/mocha

Scanned commit 79db2ee53b863813b97bf2a74cafa1599115c074 (2026-10-01). Generated by codevetter codevetter 1.15.0 on 2026-10-02.

Source inspection; upstream code and tests were not executed.

The source supports concrete traces through CLI startup, serial execution, parallel execution, browser execution, and report persistence. Startup can branch into a new Node process when runtime flags are present. Serial execution loads declarations centrally, while parallel execution defers loading to workers. Completion travels through runner events, reporter completion, and the CLI exit handler.
- CLI startup: bin/mocha.js calls loadOptions → partitions Node/Mocha options → disables timeouts for applicable debugger flags → either spawn(process.execPath, args) or main([], mochaArgs). — [bin/mocha.js#L33-L44](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/bin/mocha.js#L33-L44) [bin/mocha.js#L57-L69](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/bin/mocha.js#L57-L69) [bin/mocha.js#L86-L95](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/bin/mocha.js#L86-L95) [bin/mocha.js#L122-L163](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/bin/mocha.js#L122-L163)
- Default command preparation: main → runCommand → default spec ['test'] → normalizeRunOptions → validateRunOptions → await prepareRunOptions → handler → new Mocha(argv) → runMocha. — [lib/cli/cli.js#L80-L97](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/cli/cli.js#L80-L97) [lib/cli/cli.js#L129-L136](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/cli/cli.js#L129-L136) [lib/cli/run.cjs#L137-L146](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/cli/run.cjs#L137-L146)
- Serial loading: runMocha selects singleRun → collectFiles → assigns mocha.files → awaits loadFilesAsync → emits pre-require → executes the module → emits require/post-require → calls mocha.run. — [lib/cli/run-helpers.cjs#L159-L175](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/cli/run-helpers.cjs#L159-L175) [lib/cli/run-helpers.cjs#L241-L248](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/cli/run-helpers.cjs#L241-L248) [lib/mocha.cjs#L438-L452](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/mocha.cjs#L438-L452) [lib/nodejs/esm-utils.cjs#L180-L186](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/nodejs/esm-utils.cjs#L180-L186)
- Serial execution: Mocha.run creates runner/stats/reporter → optionally runs global setup → Runner.run filters only declarations → runSuite executes beforeAll → runTests executes beforeEach/test/afterEach → traverses children → afterAll → run end. — [lib/mocha.cjs#L994-L1005](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/mocha.cjs#L994-L1005) [lib/mocha.cjs#L1040-L1054](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/mocha.cjs#L1040-L1054) [lib/runner.js#L929-L987](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/runner.js#L929-L987) [lib/runner.js#L1002-L1071](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/runner.js#L1002-L1071) [lib/runner.js#L1205-L1231](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/runner.js#L1205-L1231)
- Failure/completion branch: a retry clones and requeues the test before afterEach; exhausted retries call fail. Mocha then attempts global teardown, adds one failure if teardown throws, and invokes reporter.done or the callback before CLI exit handling. — [lib/runner.js#L958-L981](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/runner.js#L958-L981) [lib/mocha.cjs#L1022-L1035](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/mocha.cjs#L1022-L1035) [lib/mocha.cjs#L1055-L1069](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/mocha.cjs#L1055-L1069) [lib/cli/run-helpers.cjs#L303-L307](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/cli/run-helpers.cjs#L303-L307)
- Parallel execution: parallelRun collects files without loading them → Mocha uses ParallelBufferedRunner → creates the pool → queues files → worker bootstrap loads requires/UI once → new Mocha(opts).addFile(filepath) → loadFilesAsync → run → serialize → parent replays events and terminates the pool. — [lib/cli/run-helpers.cjs#L191-L205](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/cli/run-helpers.cjs#L191-L205) [lib/nodejs/parallel-buffered-runner.cjs#L311-L340](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/nodejs/parallel-buffered-runner.cjs#L311-L340) [lib/nodejs/worker.cjs#L49-L58](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/nodejs/worker.cjs#L49-L58) [lib/nodejs/worker.cjs#L107-L139](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/nodejs/worker.cjs#L107-L139) [lib/nodejs/parallel-buffered-runner.cjs#L173-L204](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/nodejs/parallel-buffered-runner.cjs#L173-L204)
- Browser execution: browser-entry creates an HTML-reporter Mocha instance → setup calls ui → ui emits pre-require into global scope → script declarations build suites → run applies URL filters → delegates to Mocha.run → highlights code when the DOM target exists. — [browser-entry.js#L18-L18](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/browser-entry.js#L18-L18) [browser-entry.js#L130-L157](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/browser-entry.js#L130-L157) [browser-entry.js#L164-L192](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/browser-entry.js#L164-L192) [lib/interfaces/bdd.js#L27-L46](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/interfaces/bdd.js#L27-L46)
- Report persistence: JSONReporter accumulates pass/fail/pending/end events → on run end cleans objects → sets runner.testResults → JSON.stringify → mkdirSync/writeFileSync when output is configured, otherwise stdout; write failure logs an error and falls back to stdout. — [lib/reporters/json.js#L55-L95](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/reporters/json.js#L55-L95) [lib/reporters/json.js#L108-L146](https://github.com/mochajs/mocha/blob/79db2ee53b863813b97bf2a74cafa1599115c074/lib/reporters/json.js#L108-L146)

Every claim links to its cited files at the pinned commit. Citations identify source evidence; they do not establish runtime correctness. Independent CodeVetter scan — not reviewed by upstream maintainers.

## Public product links

- [CodeVetter](https://codevetter.com/)
- [Download](https://codevetter.com/download)
- [Documentation](https://codevetter.com/docs/)
- [Source](https://github.com/Codevetter/codevetter)
