The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add first test
Kiran K4bd7e978f810
diff --git a/.gitignore b/.gitignoreindex dba2ab1b6b..b3899f78dc 100644--- a/.gitignore+++ b/.gitignore@@ -42,4 +42,5 @@ next-env.d.ts.contentlayer.vscode*.csv-*.ndjson\ No newline at end of file+*.ndjson+.vitest\ No newline at end of filediff --git a/apps/web/tests/links.test.ts b/apps/web/tests/links.test.tsdeleted file mode 100644index 7d86cc1050..0000000000--- a/apps/web/tests/links.test.ts+++ /dev/null@@ -1,35 +0,0 @@-import { beforeAll, describe, expect, test } from "vitest";-import { HttpClient } from "./utils/http";-import { Link } from "@prisma/client";-import { init } from "./utils/integration";--describe("links", async () => {- const { workspace, apiKey } = await init();-- test("creates new link", async () => {- const http = new HttpClient({- baseUrl: "http://localhost:8888/api",- headers: {- Authorization: `Bearer ${apiKey}`,- },- });-- console.log("workspace", workspace);-- const res = await http.post<Link>({- path: "/links",- query: { workspaceId: workspace.workspaceId },- body: { url: "https://google.com" },- });-- expect(res.status).toEqual(200);- expect(res.data).toMatchObject({- url: "https://google.com",- domain: "kiran.localhost",- shortLink: `https://kiran.localhost/${res.data.key}`,- archived: false,- workspaceId: workspace.workspaceId,- projectId: workspace.id,- });- });-});diff --git a/apps/web/tests/links/create-link.test.ts b/apps/web/tests/links/create-link.test.tsnew file mode 100644index 0000000000..78a6e1a692--- /dev/null+++ b/apps/web/tests/links/create-link.test.ts@@ -0,0 +1,32 @@+import { expect, test } from "vitest";+import { HttpClient } from "../utils/http";+import { Link } from "@prisma/client";+import { IntegrationHarness } from "../utils/integration";++test("creates new link", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, apiKey } = await h.init();++ const http = new HttpClient({+ baseUrl: "http://localhost:8888/api",+ headers: {+ Authorization: `Bearer ${apiKey.token}`,+ },+ });++ const res = await http.post<Link>({+ path: "/links",+ query: { workspaceId: workspace.workspaceId },+ body: { url: "https://google.com" },+ });++ expect(res.status).toEqual(200);+ expect(res.data).toMatchObject({+ url: "https://google.com",+ domain: "kiran.localhost",+ shortLink: `https://kiran.localhost/${res.data.key}`,+ archived: false,+ workspaceId: workspace.workspaceId,+ projectId: workspace.id,+ });+});diff --git a/apps/web/tests/links/list-links.test.ts b/apps/web/tests/links/list-links.test.tsnew file mode 100644index 0000000000..342fa0f622--- /dev/null+++ b/apps/web/tests/links/list-links.test.ts@@ -0,0 +1,62 @@+import { expect, test } from "vitest";+import { HttpClient } from "../utils/http";+import { Link } from "@prisma/client";+import { IntegrationHarness } from "../utils/integration";++test(+ "list links",+ async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, apiKey } = await h.init();++ const http = new HttpClient({+ baseUrl: "http://localhost:8888/api",+ headers: {+ Authorization: `Bearer ${apiKey.token}`,+ },+ });++ // Create a link+ const { data: firstLink } = await http.post<Link>({+ path: "/links",+ query: { workspaceId: workspace.workspaceId },+ body: { url: "https://google.com" },+ });++ // Fake delay+ await new Promise((resolve) => setTimeout(resolve, 2000));++ // Create another link+ const { data: secondLink } = await http.post<Link>({+ path: "/links",+ query: { workspaceId: workspace.workspaceId },+ body: { url: "https://google.uk" },+ });++ // List links+ const {data: links, status} = await http.get<Link[]>({+ path: "/links",+ query: { workspaceId: workspace.workspaceId },+ });++ expect(status).toEqual(200);+ expect(links.length).toEqual(2);+ // expect(links[0]).toMatchObject({+ // url: "https://google.uk",+ // domain: "kiran.localhost",+ // shortLink: `https://kiran.localhost/${firstLink.key}`,+ // archived: false,+ // // workspaceId: workspace.workspaceId,+ // projectId: workspace.id,+ // });+ // expect(links[1]).toMatchObject({+ // url: "https://google.com",+ // domain: "kiran.localhost",+ // shortLink: `https://kiran.localhost/${secondLink.key}`,+ // archived: false,+ // // workspaceId: workspace.workspaceId,+ // projectId: workspace.id,+ // });+ },+ { timeout: 10000 },+);diff --git a/apps/web/tests/setup.ts b/apps/web/tests/setup.tsindex dd5fc8f734..e69de29bb2 100644--- a/apps/web/tests/setup.ts+++ b/apps/web/tests/setup.ts@@ -1,18 +0,0 @@-import prisma from "@/lib/prisma";-import { beforeAll } from "vitest";--beforeAll(() => {- console.log("Running beforeAll");- console.log("Running beforeAll");- console.log("Running beforeAll");- console.log("Running beforeAll");- console.log("Running beforeAll");- console.log("Running beforeAll");- // await prisma.$transaction([- // prisma.project.deleteMany(),- // prisma.token.deleteMany(),- // prisma.user.deleteMany(),- // ]);-- // await prisma.$disconnect()-});diff --git a/apps/web/tests/utils/integration.ts b/apps/web/tests/utils/integration.tsindex 0c16cffc66..d0cdd7f611 100644--- a/apps/web/tests/utils/integration.ts+++ b/apps/web/tests/utils/integration.ts@@ -1,51 +1,87 @@import { hashToken } from "@/lib/auth";import prisma from "@/lib/prisma";import { nanoid } from "@dub/utils";+import { Project, Token, User } from "@prisma/client";+import { onTestFinished, test } from "vitest";+import type { TaskContext } from "vitest";-export const init = async () => {- // Create a user- const user = await prisma.user.create({- data: {- name: "Kiran",- email: "[email protected]",- emailVerified: new Date(),- },- });-- // Create a workspace for the user- const workspace = await prisma.project.create({- data: {- name: "Dub",- slug: "dub",- users: {- create: {- userId: user.id,- role: "owner",+interface Resources {+ user: User;+ workspace: Project & { workspaceId: string };+ apiKey: Token & { token: string };+}++export class IntegrationHarness {+ private readonly ctx: TaskContext;+ public resources: Resources;++ constructor(ctx: TaskContext) {+ this.ctx = ctx+ }++ async init() {+ const resources = await this.seed();+ this.resources = resources;+ return resources;+ }++ async seed() {+ this.ctx.onTestFinished(async () => {+ await prisma.$transaction([+ prisma.project.delete({ where: { id: this.resources.workspace.id } }),+ prisma.token.delete({ where: { id: this.resources.apiKey.id } }),+ prisma.user.delete({ where: { id: this.resources.user.id } }),+ ]);++ await prisma.$disconnect();+ });++ // Create a user+ const user = await prisma.user.create({+ data: {+ name: "John",+ email: `john+${nanoid()}@dub.co`,+ emailVerified: new Date(),+ },+ });++ // Create a workspace for the user+ const workspace = await prisma.project.create({+ data: {+ name: "Dub",+ slug: `dub-${nanoid()}`,+ users: {+ create: {+ userId: user.id,+ role: "owner",+ },+ },+ billingCycleStart: new Date().getDate(),+ defaultDomains: {+ create: {},},},- billingCycleStart: new Date().getDate(),- defaultDomains: {- create: {},+ });++ // 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,},- },- });-- // Create an API key for the user- const token = nanoid(24);- await prisma.token.create({- data: {- name: "API Key",- hashedKey: hashToken(token, {- noSecret: true,- }),- partialKey: `${token.slice(0, 3)}...${token.slice(-4)}`,- userId: user.id,- },- });-- return {- user,- workspace: { ...workspace, workspaceId: `ws_${workspace.id}` },- apiKey: token,- } as const;-};+ });++ const resource = {+ user,+ workspace: { ...workspace, workspaceId: `ws_${workspace.id}` },+ apiKey: { ...apiKey, token },+ } as const;++ return resource;+ }+}diff --git a/apps/web/vitest.config.ts b/apps/web/vitest.config.tsindex 20b090540d..1296293160 100644--- a/apps/web/vitest.config.ts+++ b/apps/web/vitest.config.ts@@ -6,6 +6,7 @@ export default defineConfig({test: {dir: "./tests",reporters: ["verbose"],+ // outputFile: "./.vitest/html",globals: true,setupFiles: ["./tests/setup.ts"],},