Deterministic test isolation
Prerequisite: node-contract-and-end-to-end-testing. Objective: Reset synthetic database state. Local baseline: Node.js 22.16.0 on Linux, ESM. Each example is independent: save as main.mjs in a disposable directory and run node main.mjs. The author harness uses a five-second timeout. Static snippets are not ByteMind interactive execution.
How it works
Create an independent temporary directory, database or namespace for each test. Reset fixture data from a known seed and use finally blocks or cleanup hooks. Parallel tests must not share mutable singleton state unless the test explicitly checks concurrency. Fixed inputs and controlled synchronization are better than sleeps for proving ordering. SQLite in-memory connections provide a real relational fixture but do not replicate every PostgreSQL behavior.
Prove SQLite connection isolation
import {DatabaseSync} from 'node:sqlite';import assert from 'node:assert/strict';const a=new DatabaseSync(':memory:'),b=new DatabaseSync(':memory:');try{for(const db of [a,b])db.exec('CREATE TABLE items(id INTEGER PRIMARY KEY)');a.exec('INSERT INTO items VALUES(1)');const count=db=>db.prepare('SELECT count(*) AS n FROM items').get().n;assert.equal(count(a),1);assert.equal(count(b),0);console.log(count(a),count(b));}finally{a.close();b.close();}Expected output:
1 0Separate in-memory connections hold separate databases. Node 22 SQLite is experimental.
Use isolated temporary directories and always remove them
import {mkdtempSync,writeFileSync,readFileSync,rmSync,existsSync} from 'node:fs';import {tmpdir} from 'node:os';import {join} from 'node:path';import assert from 'node:assert/strict';const dirs=[mkdtempSync(join(tmpdir(),'bm-a-')),mkdtempSync(join(tmpdir(),'bm-b-'))];try{for(const [i,d] of dirs.entries())writeFileSync(join(d,'state.txt'),String(i));assert.equal(readFileSync(join(dirs[0],'state.txt'),'utf8'),'0');assert.equal(readFileSync(join(dirs[1],'state.txt'),'utf8'),'1');}finally{for(const d of dirs)rmSync(d,{recursive:true,force:true});}console.log(dirs.every(d=>!existsSync(d)));Expected output:
trueThe identical filenames do not collide because the directories differ and are owned by the test.
Common mistakes
Do not reset a shared production database or use a global cleanup wildcard. A transaction rollback cannot reset external effects such as already-delivered messages.
Practice
Run two isolated database tests in reversed order and show that their row counts are independent.
Reference candidate: node-deterministic-test-isolation-lookup (route unverified). Documentation: https://nodejs.org/download/release/v22.16.0/docs/api/sqlite.html. Exact-version documentation not retrieved in this batch; local fixture behavior tested.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Deterministic test isolation — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
What is the safest fixture reset scope?
Do separate SQLite :memory: connections share tables by default?
What is preferable to sleeping to wait for readiness?