Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
1.9 kB ยท 59 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import { expect } from "@std/expect";import { beforeAll, describe, test } from "@std/testing/bdd";
import { createWorkspaceFixture } from "../../test/helpers";import type { DB, ServiceContext } from "../context";import { isTx, withTransaction } from "../context";
describe("withTransaction", () => { let ctx: ServiceContext;
beforeAll(async () => { const workspace = (await createWorkspaceFixture("team")).workspace; ctx = { workspace, actor: { type: "user", userId: 1 }, }; });
test("opens a tx when ctx.db is a client", async () => { let received: DB | undefined; const result = await withTransaction(ctx, async (tx) => { received = tx; return 42; }); expect(result).toBe(42); expect(received).toBeDefined(); expect(isTx(received as DB)).toBe(true); });
test("reuses ctx.db when it is already a tx (identity preserved)", async () => { await withTransaction(ctx, async (outerTx) => { let reusedTx: DB | undefined; await withTransaction({ ...ctx, db: outerTx }, async (innerTx) => { reusedTx = innerTx; }); expect(reusedTx).toBe(outerTx); }); });
test("nested withTransaction calls all reuse the outermost tx", async () => { await withTransaction(ctx, async (level1) => { await withTransaction({ ...ctx, db: level1 }, async (level2) => { await withTransaction({ ...ctx, db: level2 }, async (level3) => { expect(level3).toBe(level1); expect(level2).toBe(level1); }); }); }); });
test("error thrown inside reused tx propagates to outer caller", async () => { const outerPromise = withTransaction(ctx, async (outerTx) => { await withTransaction({ ...ctx, db: outerTx }, async () => { throw new Error("boom"); }); }); await expect(outerPromise).rejects.toThrow("boom"); });});