From 62b18475d1da8d2fd612fe50e58b63897719e674 Mon Sep 17 00:00:00 2001 From: John <46611809+JohnAllenTech@users.noreply.github.com> Date: Mon, 21 Oct 2024 14:02:51 -0600 Subject: [PATCH] chore: added teardown script (#1162) --- e2e/teardown.ts | 27 +++++++++++++++++++++++++++ playwright.config.ts | 1 + 2 files changed, 28 insertions(+) create mode 100644 e2e/teardown.ts 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,