The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
reconfigure the test strategy
Kiran Kdf8db025be36
diff --git a/apps/web/tests/links/create-link.test.ts b/apps/web/tests/links/create-link.test.tsindex 5d9e043bce..298227deff 100644--- a/apps/web/tests/links/create-link.test.ts+++ b/apps/web/tests/links/create-link.test.ts@@ -13,6 +13,8 @@ describe("POST /links", async () => {const { workspace, apiKey, user } = await h.init();const { workspaceId, id: projectId } = workspace;+ console.log({ workspace, apiKey, user });+const http = new HttpClient({baseUrl: h.baseUrl,headers: {diff --git a/apps/web/tests/tags/create-tag-error.test.ts b/apps/web/tests/tags/create-tag-error.test.tsindex 690287ad6b..3ab241a6c8 100644--- a/apps/web/tests/tags/create-tag-error.test.ts+++ b/apps/web/tests/tags/create-tag-error.test.ts@@ -46,6 +46,7 @@ cases.forEach(({ name, body, expected }) => {test(name, async (ctx) => {const h = new IntegrationHarness(ctx);const { workspace, apiKey } = await h.init();+ const { workspaceId } = workspace;const http = new HttpClient({baseUrl: h.baseUrl,@@ -56,7 +57,7 @@ cases.forEach(({ name, body, expected }) => {const response = await http.post<Tag>({path: "/tags",- query: { workspaceId: workspace.workspaceId },+ query: { workspaceId },body,});@@ -67,6 +68,7 @@ cases.forEach(({ name, body, expected }) => {test("create tag with existing name", async (ctx) => {const h = new IntegrationHarness(ctx);const { workspace, apiKey } = await h.init();+ const { workspaceId } = workspace;const http = new HttpClient({baseUrl: h.baseUrl,@@ -77,7 +79,7 @@ test("create tag with existing name", async (ctx) => {await http.post({path: "/tags",- query: { workspaceId: workspace.workspaceId },+ query: { workspaceId },body: {tag: "news",color: "red",@@ -87,7 +89,7 @@ test("create tag with existing name", async (ctx) => {// Create the same tag againconst { status, data: error } = await http.post<Tag>({path: "/tags",- query: { workspaceId: workspace.workspaceId },+ query: { workspaceId },body: {tag: "news",color: "red",diff --git a/apps/web/tests/tags/create-tag.test.ts b/apps/web/tests/tags/create-tag.test.tsindex 55958a8544..e13fca0e59 100644--- a/apps/web/tests/tags/create-tag.test.ts+++ b/apps/web/tests/tags/create-tag.test.ts@@ -1,12 +1,15 @@import { Tag } from "@prisma/client";-import { expect, test } from "vitest";+import { expect, inject, test } from "vitest";import { HttpClient } from "../utils/http";import { IntegrationHarness } from "../utils/integration";import { expectedTag } from "../utils/schema";test("POST /tags", async (ctx) => {+ console.log("inject", inject("workspace"));+const h = new IntegrationHarness(ctx);const { workspace, apiKey } = await h.init();+ const { workspaceId } = workspace;const http = new HttpClient({baseUrl: h.baseUrl,@@ -17,7 +20,7 @@ test("POST /tags", async (ctx) => {const { status, data: tag } = await http.post<Tag>({path: "/tags",- query: { workspaceId: workspace.workspaceId },+ query: { workspaceId },body: {tag: "news",color: "red",diff --git a/apps/web/tests/utils/env.ts b/apps/web/tests/utils/env.tsindex 800ae24d35..d8329ad8a2 100644--- a/apps/web/tests/utils/env.ts+++ b/apps/web/tests/utils/env.ts@@ -2,4 +2,8 @@ import { z } from "zod";export const integrationTestEnv = z.object({API_BASE_URL: z.string().url().default("http://api.localhost:8888"),-});+ TOKEN: z.string().min(1),+ // WORKSPACE_ID: z.string().min(1),+ // WORKSPACE_SLUG: z.string().min(1),+ USER_ID: z.string().min(1),+});\ No newline at end of filediff --git a/apps/web/tests/utils/globalSetup.ts b/apps/web/tests/utils/globalSetup.tsnew file mode 100644index 0000000000..9b35a35dcc--- /dev/null+++ b/apps/web/tests/utils/globalSetup.ts@@ -0,0 +1,56 @@+import prisma from "@/lib/prisma";+import { nanoid } from "@dub/utils";+import { Project } from "@prisma/client";+import { integrationTestEnv } from "./env";++const workspaceSlug = `dub-${nanoid()}`;+let teardownHappened = false;++const { USER_ID } = integrationTestEnv.parse(process.env);++declare module "vitest" {+ export interface ProvidedContext {+ workspace: Project & { workspaceId: string };+ }+}++export async function setup({ provide }) {+ const workspace = await prisma.project.create({+ data: {+ name: "Dub",+ slug: workspaceSlug,+ plan: "pro",+ inviteCode: nanoid(6),+ billingCycleStart: new Date().getDate(),+ users: {+ create: {+ userId: USER_ID,+ role: "owner",+ },+ },+ defaultDomains: {+ create: {},+ },+ },+ });++ provide("workspace", { ...workspace, workspaceId: `ws_${workspace.id}` });+}++export async function teardown() {+ if (teardownHappened) {+ throw new Error("teardown called twice");+ }++ teardownHappened = true;++ await prisma.$transaction([+ prisma.project.deleteMany({+ where: {+ slug: workspaceSlug,+ },+ }),+ ]);++ await prisma.$disconnect();+}diff --git a/apps/web/tests/utils/integration.ts b/apps/web/tests/utils/integration.tsindex 3b7616e19a..5b052e4f19 100644--- a/apps/web/tests/utils/integration.ts+++ b/apps/web/tests/utils/integration.ts@@ -1,98 +1,74 @@-import { hashToken } from "@/lib/auth";import prisma from "@/lib/prisma";-import { nanoid } from "@dub/utils";-import { Project, Token, User } from "@prisma/client";-import type { TaskContext } from "vitest";+import { Project, User } from "@prisma/client";+import { inject, type TaskContext } from "vitest";+import { z } from "zod";import { integrationTestEnv } from "./env";interface Resources {- user: User;+ user: Pick<User, "id">;workspace: Project & { workspaceId: string };- apiKey: Token & { token: string };+ apiKey: { token: string };}export class IntegrationHarness {private readonly ctx?: TaskContext;+ private env: z.infer<typeof integrationTestEnv>;public resources: Resources;public baseUrl: string;constructor(ctx?: TaskContext) {- const env = integrationTestEnv.parse(process.env);+ this.env = integrationTestEnv.parse(process.env);this.ctx = ctx;- this.baseUrl = env.API_BASE_URL;+ this.baseUrl = this.env.API_BASE_URL;}async init() {- this.ctx?.onTestFinished(async () => {- await this.teardown();- });+ // this.ctx?.onTestFinished(async () => {+ // await this.teardown();+ // });- // Create a user- const user = await prisma.user.create({- data: {- name: "John",- email: `john+${nanoid()}@dub.co`,- emailVerified: new Date(),- },- });+ const user = {+ id: this.env.USER_ID,+ };- // Create a workspace for the user- const workspace = await prisma.project.create({- data: {- name: "Dub",- slug: `dub-${nanoid()}`,- plan: "pro",- inviteCode: nanoid(6),- billingCycleStart: new Date().getDate(),- users: {- create: {- userId: user.id,- role: "owner",- },- },- defaultDomains: {- create: {},- },- },- });+ const apiKey = {+ token: this.env.TOKEN,+ };- // Create an API key for the user- const token = nanoid(24);- const apiKey = await prisma.token.create({- data: {- name: "API Key",- hashedKey: hashToken(token, {- noSecret: true,- }),- partialKey: `${token.slice(0, 3)}...${token.slice(-4)}`,- userId: user.id,- },- });+ const workspace = inject("workspace");this.resources = {user,- workspace: { ...workspace, workspaceId: `ws_${workspace.id}` },- apiKey: { ...apiKey, token },+ workspace,+ apiKey,} as const;return this.resources;}public async teardown() {- await prisma.$transaction([- prisma.project.deleteMany({- where: {- users: {- some: {- userId: this.resources.user.id,- },- },- },- }),- prisma.user.delete({ where: { id: this.resources.user.id } }),- ]);+ // await prisma.$transaction([+ // prisma.project.deleteMany({+ // where: {+ // users: {+ // some: {+ // userId: this.resources.user.id,+ // },+ // },+ // },+ // }),+ // prisma.user.delete({ where: { id: this.resources.user.id } }),+ // ]);+ // await prisma.$disconnect();+ }- await prisma.$disconnect();+ public async cleanup(id: string) {+ console.log("Deleting workspace", id);+ await prisma.project.delete({+ where: {+ id,+ },+ });}}diff --git a/apps/web/tests/workspaces/create-workspace-error.test.ts b/apps/web/tests/workspaces/create-workspace-error.test.tsindex 5c04c87990..276b7b83b2 100644--- a/apps/web/tests/workspaces/create-workspace-error.test.ts+++ b/apps/web/tests/workspaces/create-workspace-error.test.ts@@ -1,13 +1,11 @@-import { nanoid } from "@dub/utils";import { Project } from "@prisma/client";-import slugify from "@sindresorhus/slugify";import { expect, test } from "vitest";import { HttpClient } from "../utils/http";import { IntegrationHarness } from "../utils/integration";test("should not create workspace with slug in use", async (ctx) => {const h = new IntegrationHarness(ctx);- const { apiKey } = await h.init();+ const { apiKey, workspace } = await h.init();const http = new HttpClient({baseUrl: h.baseUrl,@@ -16,27 +14,20 @@ test("should not create workspace with slug in use", async (ctx) => {},});- const name = "Dub Workspace";- const slug = slugify(`dub-${nanoid()}`);-- // Create a workspace with the same slug- const { data: workspace } = await http.post<Project>({- path: "/workspaces",- body: {- name,- slug,- },- });+ console.log("existing workspace", workspace);// Create another workspace with the same slugconst { status, data: error } = await http.post<Project>({path: "/workspaces",body: {- name,- slug,+ name: "Dub Workspace",+ slug: workspace.slug,},});+ console.log("error", error);+ console.log("status", status);+expect(status).toEqual(409);expect(error).toEqual({error: {diff --git a/apps/web/tests/workspaces/create-workspace.test.ts b/apps/web/tests/workspaces/create-workspace.test.tsindex 1c0599d806..161eada709 100644--- a/apps/web/tests/workspaces/create-workspace.test.ts+++ b/apps/web/tests/workspaces/create-workspace.test.ts@@ -1,7 +1,7 @@import { nanoid } from "@dub/utils";import { Project } from "@prisma/client";import slugify from "@sindresorhus/slugify";-import { expect, test } from "vitest";+import { afterAll, expect, test } from "vitest";import { HttpClient } from "../utils/http";import { IntegrationHarness } from "../utils/integration";import { expectedWorkspace } from "../utils/schema";@@ -36,4 +36,8 @@ test("POST /workspaces", async (ctx) => {domains: [],users: [{ role: "owner" }],});++ afterAll(async () => {+ await h.cleanup(workspace.id);+ });});diff --git a/apps/web/tests/workspaces/list-workspaces.test.ts b/apps/web/tests/workspaces/list-workspaces.test.tsindex cb90e4e5cb..dbbe71892b 100644--- a/apps/web/tests/workspaces/list-workspaces.test.ts+++ b/apps/web/tests/workspaces/list-workspaces.test.ts@@ -1,14 +1,11 @@-import { nanoid } from "@dub/utils";import { Project } from "@prisma/client";-import slugify from "@sindresorhus/slugify";import { expect, test } from "vitest";import { HttpClient } from "../utils/http";import { IntegrationHarness } from "../utils/integration";-import { expectedWorkspace } from "../utils/schema";test("GET /workspaces", async (ctx) => {const h = new IntegrationHarness(ctx);- const { apiKey, workspace } = await h.init();+ const { apiKey } = await h.init();const http = new HttpClient({baseUrl: h.baseUrl,@@ -17,37 +14,19 @@ test("GET /workspaces", async (ctx) => {},});- const workspacesToCreate = [- { name: "Personal", slug: slugify(nanoid(6)) },- { name: "Work", slug: slugify(nanoid(6)) },- ];-- await http.post({- path: "/workspaces",- body: { ...workspacesToCreate[0] },- });-- // Fetch the list of workspacesconst { status, data: workspaces } = await http.get<Project[]>({path: "/workspaces",});expect(status).toEqual(200);- expect(workspaces).toHaveLength(2);-- expect(workspaces).toMatchObject([- {- ...expectedWorkspace,- name: workspace.name,- slug: workspace.slug,- plan: "pro",- domains: [],- },- {- ...expectedWorkspace,- ...workspacesToCreate[0],- users: [{ role: "owner" }],- domains: [],- },- ]);+ expect(workspaces.length).greaterThanOrEqual(1);+ // expect(workspaces).toMatchObject([+ // {+ // ...expectedWorkspace,+ // name: workspace.name,+ // slug: workspace.slug,+ // plan: "pro",+ // domains: [],+ // },+ // ]);});diff --git a/apps/web/vitest.config.ts b/apps/web/vitest.config.tsindex a6d76589c0..283bde36a6 100644--- a/apps/web/vitest.config.ts+++ b/apps/web/vitest.config.ts@@ -8,5 +8,6 @@ export default defineConfig({reporters: ["verbose"],globals: true,testTimeout: 50000,+ globalSetup: ["./tests/utils/globalSetup.ts"],},});