CodeVetter · Repo Unpack · Behavior traces
mochajs/mocha
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).
- Default command preparation: main → runCommand → default spec ['test'] → normalizeRunOptions → validateRunOptions → await prepareRunOptions → handler → new Mocha(argv) → runMocha.
- Serial loading: runMocha selects singleRun → collectFiles → assigns mocha.files → awaits loadFilesAsync → emits pre-require → executes the module → emits require/post-require → calls mocha.run.
- 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.
- 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.
- 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.
- 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.
- 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.