diff --git a/e2e/teardown.ts b/e2e/teardown.ts new file mode 100644 index 00000000..720b1364 --- /dev/null +++ b/e2e/teardown.ts @@ -0,0 +1,27 @@ +import dotenv from "dotenv"; +import postgres from "postgres"; + +dotenv.config(); // Load .env file contents into process.env + +export const teardown = async () => { + try { + if (!process.env.DATABASE_URL || !process.env.E2E_USER_ID) + throw new Error("Missing env variables for DB clean up script"); + const db = postgres(process.env.DATABASE_URL as string); + + // the test suit adds posts created by the E2E user. We want to remove them between test runs + await db` + DELETE FROM "Post" WHERE "userId" = ${process.env.E2E_USER_ID as string} + `; + // the test suit adds comments created by the E2E user. We want to remove them between test runs + await db` + DELETE FROM "Comment" WHERE "userId" = ${process.env.E2E_USER_ID as string} + `; + + console.log("DB clean up successful"); + } catch (err) { + console.log("Error while cleaning up DB after E2E test run", err); + } +}; + +export default teardown; diff --git a/playwright.config.ts b/playwright.config.ts index 98edcf90..a58f2979 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -10,6 +10,7 @@ import { defineConfig, devices } from "@playwright/test"; * See https://playwright.dev/docs/test-configuration. */ export default defineConfig({ + globalTeardown: "./e2e/teardown.ts", testDir: "e2e", /* Run tests in files in parallel */ fullyParallel: true,