Skip to content

Commit c2e6c6d

Browse files
committed
fix: move test lifecycle hooks inside describe block to fix flaky CI
beforeAll/afterAll/afterEach were at module scope, outside the describe block. In CI the DB connection isn't ready at module-level hook time, causing "Cannot read properties of undefined (reading 'users')".
1 parent 1249f11 commit c2e6c6d

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/lib/migrations/routines/09-delete-empty-conversations.spec.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Conversation } from "$lib/types/Conversation";
44
import { ObjectId } from "mongodb";
55
import { deleteConversations } from "./09-delete-empty-conversations";
66
import { afterAll, afterEach, beforeAll, describe, expect, test } from "vitest";
7-
import { collections } from "$lib/server/database";
7+
import { collections, ready } from "$lib/server/database";
88

99
type Message = Conversation["messages"][number];
1010

@@ -190,25 +190,26 @@ describe.sequential("Deleting discarded conversations", async () => {
190190

191191
expect(result).toBe(10010);
192192
});
193-
});
194193

195-
beforeAll(async () => {
196-
await collections.users.insertOne(userData);
197-
await collections.sessions.insertOne(sessionForUser);
198-
}, 20000);
194+
beforeAll(async () => {
195+
await ready;
196+
await collections.users.insertOne(userData);
197+
await collections.sessions.insertOne(sessionForUser);
198+
}, 20000);
199199

200-
afterAll(async () => {
201-
await collections.users.deleteOne({
202-
_id: userData._id,
203-
});
204-
await collections.sessions.deleteOne({
205-
_id: sessionForUser._id,
200+
afterAll(async () => {
201+
await collections.users.deleteOne({
202+
_id: userData._id,
203+
});
204+
await collections.sessions.deleteOne({
205+
_id: sessionForUser._id,
206+
});
207+
await collections.conversations.deleteMany({});
206208
});
207-
await collections.conversations.deleteMany({});
208-
});
209209

210-
afterEach(async () => {
211-
await collections.conversations.deleteMany({
212-
_id: { $in: [conversationBase._id] },
210+
afterEach(async () => {
211+
await collections.conversations.deleteMany({
212+
_id: { $in: [conversationBase._id] },
213+
});
213214
});
214215
});

0 commit comments

Comments
 (0)