Mocha --parallel: test files collide on shared ports and database state when run across workers
0 reputation · 20 Jun 2026, 22:05 UTC
0 reputation · 20 Jun 2026, 22:05 UTC
Mocha's --parallel mode distributes test files across a pool of worker processes, each loading its files in isolation. That isolation ends at the process boundary: any external resource a suite touches — a TCP port, a database schema, a temp directory — is still shared by every worker on the machine.
The design goal here is to keep an existing suite green under --parallel without rewriting tests, where several files currently assume exclusive access to a fixed port and a single seeded database. Root-level hooks run once per worker process, so setup that was safe in serial mode now executes concurrently, and Mocha itself provides no built-in coordination primitive for namespacing resources between workers.
The uncertainty is which isolation strategy fits Mocha's scheduling model: deriving per-worker namespaces from something stable like the worker's process ID or an environment variable Mocha exposes, versus partitioning resources statically per test file, versus simply accepting serial mode for the coupled subset. Buffered per-file output also makes contention failures harder to attribute.
Assuming a recent Mocha major version (to be confirmed with npx mocha --version):
--parallel (reporters, exclusive tests) that should rule it out for suites with shared fixtures?A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.