Add test infrastructure with coverage and DB test skipping
- Create @reviq/test-helpers package with shared test utilities - Add describeE2E helper that auto-prefixes test names with [e2e] - Support SKIP_DB_TESTS=1 to skip database-dependent tests - Add unix socket support for TEST_DATABASE_URL - Add root commands: test:unit, test:all, test:cov, test:unit:cov - Configure bunfig.toml to exclude dist/ from coverage reports - Clean up tsconfig.json files to remove redundant settings Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
60
packages/testing/test-helpers/src/test-transaction.ts
Normal file
60
packages/testing/test-helpers/src/test-transaction.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* Transaction-based test isolation helper
|
||||
*
|
||||
* Wraps test code in a transaction that auto-rollbacks, providing
|
||||
* fast test isolation without truncating tables between tests.
|
||||
*/
|
||||
|
||||
import type { Database } from "@reviq/db-schema";
|
||||
import type { Kysely } from "kysely";
|
||||
|
||||
/**
|
||||
* Signal used to trigger transaction rollback after test completes
|
||||
*/
|
||||
class RollbackSignal extends Error {
|
||||
constructor() {
|
||||
super("RollbackSignal");
|
||||
this.name = "RollbackSignal";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a test function inside a transaction that auto-rollbacks.
|
||||
*
|
||||
* The transaction implements the same interface as Kysely<Database>,
|
||||
* so it can be passed to context builders and used for all queries.
|
||||
* After the test completes, the transaction is rolled back, providing
|
||||
* instant cleanup without truncating tables.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* test("creates user", async () => {
|
||||
* await withTestTransaction(getSharedDb(), async (db) => {
|
||||
* const user = await createTestUser(db, { email: "test@example.com" });
|
||||
* const ctx = createAPIContext({ db });
|
||||
* // ... test code
|
||||
* }); // Auto-rollback here
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
export async function withTestTransaction<T>(
|
||||
db: Kysely<Database>,
|
||||
testFn: (trx: Kysely<Database>) => Promise<T>,
|
||||
): Promise<T | undefined> {
|
||||
let result: T | undefined;
|
||||
|
||||
try {
|
||||
await db.transaction().execute(async (trx) => {
|
||||
result = await testFn(trx);
|
||||
// Force rollback by throwing after test completes successfully
|
||||
throw new RollbackSignal();
|
||||
});
|
||||
} catch (e) {
|
||||
// Swallow the rollback signal - this is expected behavior
|
||||
if (!(e instanceof RollbackSignal)) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user