Skip to content

Commit

Permalink
fix(tests): race condition in local environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirt committed Nov 21, 2024
1 parent dc6be7e commit b65de4e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "prettier --check .",
"format": "prettier --write .",
"seed": "tsx src/seed.ts",
"test": "NODE_ENV=test firebase emulators:exec --only auth,storage,firestore --project tutortoise-test 'vitest'"
"test": "NODE_ENV=test firebase emulators:exec --only auth,storage,database,firestore --project tutortoise-test 'vitest'"
},
"dependencies": {
"@google-cloud/storage": "^7.14.0",
Expand Down
34 changes: 17 additions & 17 deletions tests/integration/chat.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { auth, firestore, bucket, realtimeDb } from "@/config";
import { beforeEach, afterAll, vi } from "vitest";
import { beforeEach } from "vitest";
import { app } from "@/main";
import { faker } from "@faker-js/faker";
import { initializeApp } from "firebase/app";
Expand All @@ -25,17 +25,17 @@ const chatService = new ChatService({
});
const authService = new AuthService({ auth, firestore, fcmService });

async function cleanupCollections() {
const collections = ["chat_rooms", "chat_messages", "learners", "tutors"];
for (const collection of collections) {
const snapshot = await firestore.collection(collection).get();
const batch = firestore.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();
}
}
// async function cleanupCollections() {
// const collections = ["chat_rooms", "chat_messages", "learners", "tutors"];
// for (const collection of collections) {
// const snapshot = await firestore.collection(collection).get();
// const batch = firestore.batch();
// snapshot.docs.forEach((doc) => {
// batch.delete(doc.ref);
// });
// await batch.commit();
// }
// }

const firebaseApp = initializeApp({
apiKey: "test-api-key",
Expand Down Expand Up @@ -104,8 +104,8 @@ describe("Chat Features", () => {
let roomId: string;

beforeAll(async () => {
await cleanupCollections();

// await cleanupCollections();
//
[learner, tutor] = await Promise.all([
createTestUser("learner"),
createTestUser("tutor"),
Expand All @@ -122,9 +122,9 @@ describe("Chat Features", () => {
}
});

afterAll(async () => {
await cleanupCollections();
});
// afterAll(async () => {
// await cleanupCollections();
// });

describe("Create chat room", () => {
test("should create a new chat room", async () => {
Expand Down
36 changes: 18 additions & 18 deletions tests/integration/fcm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
signInWithCustomToken,
} from "firebase/auth";
import supertest from "supertest";
import { beforeAll, beforeEach, describe, expect, test } from "vitest";
import { describe, expect, test } from "vitest";

const firebaseApp = initializeApp({
apiKey: "test-api-key",
Expand All @@ -24,17 +24,17 @@ async function getIdToken(userId: string) {
return user.getIdToken();
}

async function cleanupCollections() {
const collections = ["user_fcm_tokens", "learners", "tutors"];
for (const collection of collections) {
const snapshot = await firestore.collection(collection).get();
const batch = firestore.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();
}
}
// async function cleanupCollections() {
// const collections = ["user_fcm_tokens", "learners", "tutors"];
// for (const collection of collections) {
// const snapshot = await firestore.collection(collection).get();
// const batch = firestore.batch();
// snapshot.docs.forEach((doc) => {
// batch.delete(doc.ref);
// });
// await batch.commit();
// }
// }

async function registerUser(role: "learner" | "tutor") {
const userData = {
Expand All @@ -57,13 +57,13 @@ async function registerUser(role: "learner" | "tutor") {
}

describe("FCM Token Management", () => {
beforeAll(async () => {
await cleanupCollections();
});
// beforeAll(async () => {
// await cleanupCollections();
// });

beforeEach(async () => {
await cleanupCollections();
});
// beforeEach(async () => {
// await cleanupCollections();
// });

describe("Store FCM Token", () => {
test("should store FCM token for authenticated user", async () => {
Expand Down

0 comments on commit b65de4e

Please sign in to comment.