Skip to content

Commit

Permalink
moved database clearing into global-teardown.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyAsh5114 committed Dec 23, 2023
1 parent 5185d9f commit 7eb3a2a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
4 changes: 3 additions & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,7 @@ export default defineConfig({
command: "npm run build && npm run preview",
port: 4173,
reuseExistingServer: !process.env.CI
}
},
globalSetup: './tests/global-teardown',
globalTeardown: './tests/global-teardown'
});
18 changes: 0 additions & 18 deletions tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,8 @@
import { test as baseTest } from "@playwright/test";
import path from "path";
import dotenv from "dotenv";
import clientPromise from "$lib/mongo/mongodb";
import type { ObjectId } from "mongodb";
dotenv.config();

async function clearUserData(sessionToken: string) {
const client = await clientPromise;
const sessionDocument = await client.db().collection("sessions").findOne({ sessionToken });
if (!sessionDocument) return;

// Clear all test user generated data
const userId = sessionDocument.userId as ObjectId;
await client.db().collection("mesocycles").deleteMany({ userId });
await client.db().collection("mesocycleTemplates").deleteMany({ userId });
await client.db().collection("workouts").deleteMany({ userId });
await client.db().collection("userPreferences").deleteOne({ userId });
}

export * from "@playwright/test";
export const test = baseTest.extend<{}, { workerStorageState: string }>({
// Use the same storage state for all tests in this worker.
Expand Down Expand Up @@ -52,9 +37,6 @@ export const test = baseTest.extend<{}, { workerStorageState: string }>({
await page.context().storageState({ path: fileName });
await page.close();
await use(fileName);

// Clear test user state
await clearUserData(testSession);
},
{ scope: "worker" }
]
Expand Down
27 changes: 27 additions & 0 deletions tests/global-teardown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import clientPromise from "$lib/mongo/mongodb";
import type { ObjectId } from "mongodb";
import dotenv from "dotenv";
import type { FullConfig } from "@playwright/test";
dotenv.config();

async function globalTeardown(config: FullConfig) {
const client = await clientPromise;

for (let i = 1; i <= config.workers; i++) {
console.log("clearing test user" + i + " data");
const sessionToken = process.env[`TEST_SESSION_${i}`];
if (!sessionToken) break;

const sessionDocument = await client.db().collection("sessions").findOne({ sessionToken });
if (!sessionDocument) break;

// Clear all test user generated data
const userId = sessionDocument.userId as ObjectId;
await client.db().collection("mesocycles").deleteMany({ userId });
await client.db().collection("mesocycleTemplates").deleteMany({ userId });
await client.db().collection("workouts").deleteMany({ userId });
await client.db().collection("userPreferences").deleteOne({ userId });
}
}

export default globalTeardown;

0 comments on commit 7eb3a2a

Please sign in to comment.