test and production_master must never use the same DB
Sonamu automatically validates this during initialization
// sonamu/src/testing/fixture-manager.tsif (Sonamu.dbConfig.test && Sonamu.dbConfig.production_master) { const tConn = Sonamu.dbConfig.test.connection; const pConn = Sonamu.dbConfig.production_master.connection; if ( `${tConn.host ?? "localhost"}:${tConn.port ?? 5432}/${tConn.database}` === `${pConn.host ?? "localhost"}:${pConn.port ?? 5432}/${pConn.database}` ) { throw new Error("Test DB and Production DB use the same database."); }}
// sonamu/src/testing/bootstrap.tsexport function bootstrap(vi: VitestUtils) { beforeEach(async () => { // Before each test: Start transaction await DB.createTestTransaction(); }); afterEach(async () => { // After each test: Auto rollback await DB.clearTestTransaction(); });}
Flow:
// Test 1 startsBEGIN TRANSACTION; INSERT INTO users (...); // Create test data SELECT * FROM users; // Test verificationROLLBACK; // Auto rollback - data disappears// Test 2 startsBEGIN TRANSACTION; // Starts with clean DB state INSERT INTO posts (...);ROLLBACK;
test("create user", async () => { const userModel = new UserModel(); await userModel.create({ username: "john" }); // Auto deleted after test});test("next test has clean DB", async () => { const userModel = new UserModel(); const { users } = await userModel.getUsers(); expect(users).toHaveLength(0); // john from previous test is gone});
Cautions when using test DB: 1. Never use production DB: test DB must be completely
separate from production 2. Schema sync: Run fixture init after migrations 3.
Transaction-based: Isolation between tests is handled automatically 4. Shared Fixture:
Recommend using same Fixture DB with team 5. Regular initialization: Re-initialize when dev DB
schema changes