A normal rollback test
The most important example is intentionally uneventful: write through the real application database, assert the state, and omit cleanup.
ts
it("a test's writes are visible to it...", async () => {
await db.update(users).set({ name: "Changed" }).where(eq(users.id, fx.users.olivia.id));
await userFactory.create({ name: "RolledBack" });
const [olivia] = await db.select().from(users).where(eq(users.id, fx.users.olivia.id));
expect(olivia.name).toBe("Changed");
});The next test sees the committed baseline again:
ts
it("...and gone in the next test", async () => {
const [olivia] = await db.select().from(users).where(eq(users.id, fx.users.olivia.id));
expect(olivia.name).toBe("Olivia Owner");
expect(await db.select().from(users).where(eq(users.name, "RolledBack"))).toHaveLength(0);
});The test did not reset the user or delete the factory record. The surrounding transaction was discarded.
What wraps the test
ts
aroundEach(async (runTest) => {
try {
await inRolledBackTransaction(runTest);
} finally {
signOut();
}
});Inside the harness, a private error forces rollback because an interactive transaction callback that returns normally would commit:
ts
await committedDb().transaction(async (tx) => {
current = tx as unknown as TestDatabase;
try {
await runTest();
} finally {
current = undefined;
}
throw new Rollback();
});