Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Refactor test cleanup methods: Replace `afterAll` with `onTestFinished` in multiple test files to improve resource management and ensure proper cleanup after tests. This change enhances test reliability by ensuring that resources are released immediately after each test completes.

Kiran K2b79badefa8f
diff --git a/apps/web/tests/domains/index.test.ts b/apps/web/tests/domains/index.test.ts
index fe306a52c8..67ce23d2af 100644
--- a/apps/web/tests/domains/index.test.ts
+++ b/apps/web/tests/domains/index.test.ts
@@ -1,5 +1,5 @@
import { Domain } from "@dub/prisma/client";
-import { afterAll, describe, expect, test } from "vitest";
+import { describe, expect, onTestFinished, test } from "vitest";
import { randomId } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
@@ -31,7 +31,7 @@ describe.sequential("/domains/**", async () => {
const h = new IntegrationHarness();
const { workspace, http } = await h.init();
- afterAll(async () => {
+ onTestFinished(async () => {
await h.deleteDomain(domainRecord.slug);
});
diff --git a/apps/web/tests/links/bulk-create-link.test.ts b/apps/web/tests/links/bulk-create-link.test.ts
index 179c226add..ab0abe867c 100644
--- a/apps/web/tests/links/bulk-create-link.test.ts
+++ b/apps/web/tests/links/bulk-create-link.test.ts
@@ -1,6 +1,6 @@
import z from "@/lib/zod";
import { Link } from "@dub/prisma/client";
-import { expect, test } from "vitest";
+import { expect, onTestFinished, test } from "vitest";
import { randomId } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
import { E2E_LINK } from "../utils/resource";
@@ -14,6 +14,10 @@ test("POST /links/bulk", async (ctx) => {
const workspaceId = workspace.id;
const projectId = workspaceId.replace("ws_", "");
+ onTestFinished(async () => {
+ await Promise.all([h.deleteLink(links[0].id), h.deleteLink(links[1].id)]);
+ });
+
const bulkLinks = Array.from({ length: 2 }, () => ({
url: `https://example.com/${randomId()}`,
domain,
@@ -48,6 +52,4 @@ test("POST /links/bulk", async (ctx) => {
qrCode: `https://api.dub.co/qr?url=https://${domain}/${secondLink?.key}?qr=1`,
});
expect(z.array(LinkSchema.strict()).parse(links)).toBeTruthy();
-
- await Promise.all([h.deleteLink(links[0].id), h.deleteLink(links[1].id)]);
});
diff --git a/apps/web/tests/links/bulk-update-link.test.ts b/apps/web/tests/links/bulk-update-link.test.ts
index a7982954e6..bf0e02651c 100644
--- a/apps/web/tests/links/bulk-update-link.test.ts
+++ b/apps/web/tests/links/bulk-update-link.test.ts
@@ -1,5 +1,5 @@
import { Link, Tag } from "@dub/prisma/client";
-import { afterAll, expect, test } from "vitest";
+import { expect, onTestFinished, test } from "vitest";
import { randomId, randomTagName } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
import { E2E_LINK } from "../utils/resource";
@@ -13,6 +13,14 @@ test("PATCH /links/bulk", async (ctx) => {
const workspaceId = workspace.id;
const projectId = workspaceId.replace("ws_", "");
+ onTestFinished(async () => {
+ await Promise.all([
+ h.deleteLink(createdLinks[0].id),
+ h.deleteLink(createdLinks[1].id),
+ h.deleteTag(tag.id),
+ ]);
+ });
+
const { data: createdLinks } = await http.post<Link[]>({
path: "/links/bulk",
query: { workspaceId },
@@ -59,7 +67,7 @@ test("PATCH /links/bulk", async (ctx) => {
});
expect(status).toEqual(200);
- expect(links).toHaveLength(3);
+ expect(links).toHaveLength(2);
// first link should be updated
expect(links[0]).toStrictEqual({
@@ -93,12 +101,4 @@ test("PATCH /links/bulk", async (ctx) => {
code: "unprocessable_entity",
link: expect.any(Object),
});
-
- afterAll(async () => {
- await Promise.all([
- h.deleteLink(createdLinks[0].id),
- h.deleteLink(createdLinks[1].id),
- h.deleteTag(tag.id),
- ]);
- });
});
diff --git a/apps/web/tests/links/create-link.test.ts b/apps/web/tests/links/create-link.test.ts
index 60771297c6..ef9a3eae33 100644
--- a/apps/web/tests/links/create-link.test.ts
+++ b/apps/web/tests/links/create-link.test.ts
@@ -1,6 +1,6 @@
import { Link, Tag } from "@dub/prisma/client";
import { IntegrationHarnessOld } from "tests/utils/integration-old";
-import { afterAll, describe, expect, test } from "vitest";
+import { describe, expect, onTestFinished, test } from "vitest";
import { randomId, randomTagName } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
import { E2E_LINK, E2E_WEBHOOK_ID } from "../utils/resource";
@@ -42,9 +42,13 @@ describe.sequential("POST /links", async () => {
});
});
- test("default domain", async () => {
+ test("default domain", async ({ onTestFinished }) => {
const externalId = randomId();
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
const { status, data: link } = await http.post<Link>({
path: "/links",
body: {
@@ -70,15 +74,15 @@ describe.sequential("POST /links", async () => {
qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,
});
expect(LinkSchema.strict().parse(link)).toBeTruthy();
-
- afterAll(async () => {
- await h.deleteLink(link.id);
- });
});
- test("user defined key", async () => {
+ test("user defined key", async ({ onTestFinished }) => {
const key = randomId();
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
const { status, data: link } = await http.post<Link>({
path: "/links",
body: {
@@ -100,15 +104,15 @@ describe.sequential("POST /links", async () => {
qrCode: `https://api.dub.co/qr?url=https://${domain}/${key}?qr=1`,
});
expect(LinkSchema.strict().parse(link)).toBeTruthy();
-
- afterAll(async () => {
- await h.deleteLink(link.id);
- });
});
test("prefix", async () => {
const prefix = "gh";
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
const { status, data: link } = await http.post<
Link & { shortLink: string }
>({
@@ -133,10 +137,6 @@ describe.sequential("POST /links", async () => {
qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,
});
expect(LinkSchema.strict().parse(link)).toBeTruthy();
-
- afterAll(async () => {
- await h.deleteLink(link.id);
- });
});
test("utm builder", async () => {
@@ -153,6 +153,10 @@ describe.sequential("POST /links", async () => {
longUrl.searchParams.set(key, utm[key]);
});
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
const { status, data: link } = await http.post<Link>({
path: "/links",
body: {
@@ -173,15 +177,15 @@ describe.sequential("POST /links", async () => {
qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,
});
expect(LinkSchema.strict().parse(link)).toBeTruthy();
-
- afterAll(async () => {
- await h.deleteLink(link.id);
- });
});
test("password protection", async () => {
const password = "link-password";
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
const { status, data: link } = await http.post<Link>({
path: "/links",
body: {
@@ -203,16 +207,16 @@ describe.sequential("POST /links", async () => {
qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,
});
expect(LinkSchema.strict().parse(link)).toBeTruthy();
-
- afterAll(async () => {
- await h.deleteLink(link.id);
- });
});
test("link expiration", async () => {
const expiresAt = new Date("2030-04-16T17:00:00.000Z");
const expiredUrl = "https://github.com/expired";
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
const { status, data: link } = await http.post<Link>({
path: "/links",
body: {
@@ -236,10 +240,6 @@ describe.sequential("POST /links", async () => {
qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,
});
expect(LinkSchema.strict().parse(link)).toBeTruthy();
-
- afterAll(async () => {
- await h.deleteLink(link.id);
- });
});
test("device targeting", async () => {
@@ -247,6 +247,10 @@ describe.sequential("POST /links", async () => {
const android =
"https://play.google.com/store/apps/details?id=com.disney.disneyplus";
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
const { status, data: link } = await http.post<Link>({
path: "/links",
body: {
@@ -270,10 +274,6 @@ describe.sequential("POST /links", async () => {
qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,
});
expect(LinkSchema.strict().parse(link)).toBeTruthy();
-
- afterAll(async () => {
- await h.deleteLink(link.id);
- });
});
test("geo targeting", async () => {
@@ -283,6 +283,10 @@ describe.sequential("POST /links", async () => {
DZ: `${url}/DZ`,
};
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
const { status, data: link } = await http.post<Link>({
path: "/links",
body: {
@@ -304,10 +308,6 @@ describe.sequential("POST /links", async () => {
qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,
});
expect(LinkSchema.strict().parse(link)).toBeTruthy();
-
- afterAll(async () => {
- await h.deleteLink(link.id);
- });
});
test("tags", async () => {
@@ -334,6 +334,13 @@ describe.sequential("POST /links", async () => {
};
});
+ onTestFinished(async () => {
+ await Promise.all([
+ ...tagIds.map((id) => h.deleteTag(id)),
+ h.deleteLink(link.id),
+ ]);
+ });
+
const { status, data: link } = await http.post<Link & { tags: [] }>({
path: "/links",
body: {
@@ -357,19 +364,16 @@ describe.sequential("POST /links", async () => {
tags: expect.arrayContaining(tags),
});
expect(LinkSchema.strict().parse(link)).toBeTruthy();
-
- afterAll(async () => {
- await Promise.all([
- ...tagIds.map((id) => h.deleteTag(id)),
- h.deleteLink(link.id),
- ]);
- });
});
test("custom social media cards", async () => {
const title = "custom title";
const description = "custom description";
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
const { status, data: link } = await http.post<Link>({
path: "/links",
body: {
@@ -393,13 +397,13 @@ describe.sequential("POST /links", async () => {
qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,
});
expect(LinkSchema.strict().parse(link)).toBeTruthy();
+ });
- afterAll(async () => {
+ test("webhooks", async () => {
+ onTestFinished(async () => {
await h.deleteLink(link.id);
});
- });
- test("webhooks", async () => {
const { status, data: link } = await http.post<Link & { tags: [] }>({
path: "/links",
body: {
@@ -421,10 +425,6 @@ describe.sequential("POST /links", async () => {
webhookIds: [E2E_WEBHOOK_ID],
});
expect(LinkSchema.strict().parse(link)).toBeTruthy();
-
- afterAll(async () => {
- await h.deleteLink(link.id);
- });
});
});
@@ -435,6 +435,10 @@ describe.sequential("POST /links?workspaceId=xxx", async () => {
const projectId = workspaceId.replace("ws_", "");
test("create link with old personal API keys approach", async () => {
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
const { status, data: link } = await http.post<Link>({
path: "/links",
body: {
@@ -454,9 +458,5 @@ describe.sequential("POST /links?workspaceId=xxx", async () => {
qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,
});
expect(LinkSchema.strict().parse(link)).toBeTruthy();
-
- afterAll(async () => {
- await h.deleteLink(link.id);
- });
});
});
diff --git a/apps/web/tests/links/list-links.test.ts b/apps/web/tests/links/list-links.test.ts
index b60561b956..7332787fc9 100644
--- a/apps/web/tests/links/list-links.test.ts
+++ b/apps/web/tests/links/list-links.test.ts
@@ -1,5 +1,5 @@
import { Link } from "@dub/prisma/client";
-import { afterAll, expect, test } from "vitest";
+import { expect, onTestFinished, test } from "vitest";
import { IntegrationHarness } from "../utils/integration";
import { E2E_LINK } from "../utils/resource";
@@ -11,6 +11,10 @@ test("GET /links", async (ctx) => {
const workspaceId = workspace.id;
const projectId = workspaceId.replace("ws_", "");
+ onTestFinished(async () => {
+ await h.deleteLink(firstLink.id);
+ });
+
const { data: firstLink } = await http.post<Link>({
path: "/links",
body: { url, domain },
@@ -34,8 +38,4 @@ test("GET /links", async (ctx) => {
shortLink: `https://${domain}/${firstLink.key}`,
qrCode: `https://api.dub.co/qr?url=https://${domain}/${firstLink.key}?qr=1`,
});
-
- afterAll(async () => {
- await h.deleteLink(firstLink.id);
- });
});
diff --git a/apps/web/tests/links/retrieve-link.test.ts b/apps/web/tests/links/retrieve-link.test.ts
index 824f33bd88..5b36ac535b 100644
--- a/apps/web/tests/links/retrieve-link.test.ts
+++ b/apps/web/tests/links/retrieve-link.test.ts
@@ -1,6 +1,6 @@
import { Link } from "@dub/prisma/client";
import { expectedLink } from "tests/utils/schema";
-import { afterAll, describe, expect, test } from "vitest";
+import { describe, expect, onTestFinished, test } from "vitest";
import { randomId } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
import { E2E_LINK } from "../utils/resource";
@@ -15,6 +15,10 @@ describe.sequential("GET /links/{linkId}", async () => {
const externalId = randomId();
const key = randomId();
+ onTestFinished(async () => {
+ await h.deleteLink(newLink.id);
+ });
+
const { data: newLink } = await http.post<Link>({
path: "/links",
body: {
@@ -25,10 +29,6 @@ describe.sequential("GET /links/{linkId}", async () => {
},
});
- afterAll(async () => {
- await h.deleteLink(newLink.id);
- });
-
test("by linkId", async () => {
const { status, data: link } = await http.get<Link>({
path: `/links/${newLink.id}`,
@@ -66,6 +66,10 @@ describe.sequential("GET /links/info", async () => {
const externalId = randomId();
const key = randomId();
+ onTestFinished(async () => {
+ await h.deleteLink(newLink.id);
+ });
+
const { data: newLink } = await http.post<Link>({
path: "/links",
body: {
@@ -76,10 +80,6 @@ describe.sequential("GET /links/info", async () => {
},
});
- afterAll(async () => {
- await h.deleteLink(newLink.id);
- });
-
test("by domain and key", async () => {
const { status, data: link } = await http.get<Link>({
path: "/links/info",
diff --git a/apps/web/tests/links/update-link.test.ts b/apps/web/tests/links/update-link.test.ts
index 9f71067815..1022cd1945 100644
--- a/apps/web/tests/links/update-link.test.ts
+++ b/apps/web/tests/links/update-link.test.ts
@@ -1,5 +1,5 @@
import { Link } from "@dub/prisma/client";
-import { afterAll, describe, expect, test } from "vitest";
+import { describe, expect, onTestFinished, test } from "vitest";
import { randomId } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
import { E2E_LINK } from "../utils/resource";
@@ -14,6 +14,10 @@ describe.sequential("PATCH /links/{linkId}", async () => {
const projectId = workspaceId.replace("ws_", "");
const externalId = randomId();
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
const { data: link } = await http.post<Link>({
path: "/links",
body: {
@@ -40,10 +44,6 @@ describe.sequential("PATCH /links/{linkId}", async () => {
},
};
- afterAll(async () => {
- await h.deleteLink(link.id);
- });
-
test("update link using linkId", async () => {
const { data: updatedLink } = await http.patch<Link>({
path: `/links/${link.id}`,
@@ -189,6 +189,10 @@ describe.sequential(
const projectId = workspaceId.replace("ws_", "");
const externalId = randomId();
+ onTestFinished(async () => {
+ await h.deleteLink(link.id);
+ });
+
const { data: link } = await http.post<Link>({
path: "/links",
body: {
@@ -215,10 +219,6 @@ describe.sequential(
},
};
- afterAll(async () => {
- await h.deleteLink(link.id);
- });
-
test("update link using PUT", async () => {
const { data: updatedLink } = await http.put<Link>({
path: `/links/${link.id}`,
diff --git a/apps/web/tests/links/upsert-link.test.ts b/apps/web/tests/links/upsert-link.test.ts
index f552845953..b6357358b6 100644
--- a/apps/web/tests/links/upsert-link.test.ts
+++ b/apps/web/tests/links/upsert-link.test.ts
@@ -1,5 +1,5 @@
import { Link } from "@dub/prisma/client";
-import { afterAll, describe, expect, test } from "vitest";
+import { describe, expect, onTestFinished, test } from "vitest";
import { randomId } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
import { E2E_LINK } from "../utils/resource";
@@ -15,7 +15,7 @@ describe.sequential("PUT /links/upsert", async () => {
const projectId = workspaceId.replace("ws_", "");
let createdLink: Link;
- afterAll(async () => {
+ onTestFinished(async () => {
await h.deleteLink(createdLink.id);
});
diff --git a/apps/web/tests/tags/create-tag.test.ts b/apps/web/tests/tags/create-tag.test.ts
index 7d75e31455..4dd7054308 100644
--- a/apps/web/tests/tags/create-tag.test.ts
+++ b/apps/web/tests/tags/create-tag.test.ts
@@ -1,5 +1,5 @@
import { Tag } from "@dub/prisma/client";
-import { afterAll, expect, test } from "vitest";
+import { expect, onTestFinished, test } from "vitest";
import { randomTagName } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
@@ -7,6 +7,10 @@ test("POST /tags", async (ctx) => {
const h = new IntegrationHarness(ctx);
const { http } = await h.init();
+ onTestFinished(async () => {
+ await h.deleteTag(tag.id);
+ });
+
const newTag = {
name: randomTagName(),
color: "red",
@@ -22,8 +26,4 @@ test("POST /tags", async (ctx) => {
id: expect.any(String),
...newTag,
});
-
- afterAll(async () => {
- await h.deleteTag(tag.id);
- });
});
diff --git a/apps/web/tests/tags/list-tags.test.ts b/apps/web/tests/tags/list-tags.test.ts
index ef0dd378cc..7aceca2cac 100644
--- a/apps/web/tests/tags/list-tags.test.ts
+++ b/apps/web/tests/tags/list-tags.test.ts
@@ -1,5 +1,5 @@
import { Tag } from "@dub/prisma/client";
-import { expect, test } from "vitest";
+import { expect, onTestFinished, test } from "vitest";
import { randomTagName } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
@@ -7,6 +7,10 @@ test("GET /tags", async (ctx) => {
const h = new IntegrationHarness(ctx);
const { http } = await h.init();
+ onTestFinished(async () => {
+ await h.deleteTag(tagCreated.id);
+ });
+
const newTag = {
tag: randomTagName(),
color: "red",
@@ -31,6 +35,4 @@ test("GET /tags", async (ctx) => {
},
]),
);
-
- await h.deleteTag(tagCreated.id);
});
diff --git a/apps/web/tests/utils/integration.ts b/apps/web/tests/utils/integration.ts
index 479483b1ec..294a9dc3f1 100644
--- a/apps/web/tests/utils/integration.ts
+++ b/apps/web/tests/utils/integration.ts
@@ -56,6 +56,8 @@ export class IntegrationHarness {
// Delete link
public async deleteLink(id: string) {
+ if (!id) return;
+
await this.http.delete({
path: `/links/${id}`,
});
@@ -63,6 +65,8 @@ export class IntegrationHarness {
// Delete tag
public async deleteTag(id: string) {
+ if (!id) return;
+
await this.http.delete({
path: `/tags/${id}`,
});