Skip to content

Commit 86a59e3

Browse files
committed
refactor: beforeEach beforeAll return cleanup hook
1 parent 5e2349c commit 86a59e3

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/mongodb/setupFile.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Db, MongoClient } from "mongodb";
2-
import { afterAll, afterEach, beforeAll, beforeEach, inject } from "vitest";
2+
import { beforeAll, beforeEach, inject } from "vitest";
33
// hack to keep imported vitest types
44
export type { TestContext } from "vitest";
55
// hack to fix pnpm build issue
@@ -21,18 +21,17 @@ beforeAll(async () => {
2121

2222
mongoClient = new MongoClient(uri);
2323
await mongoClient.connect();
24-
});
2524

26-
afterAll(async () => {
27-
await mongoClient.close();
25+
return async () => {
26+
await mongoClient.close();
27+
};
2828
});
2929

3030
beforeEach(async (context) => {
3131
const db = mongoClient.db(randomUUID());
3232
context.db = db;
3333
context.mongoClient = mongoClient;
34-
});
35-
36-
afterEach(async (context) => {
37-
await context.db.dropDatabase();
34+
return async () => {
35+
await context.db.dropDatabase();
36+
};
3837
});

src/mongoose/setupFile.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type Connection, createConnection } from "mongoose";
2-
import { afterAll, afterEach, beforeAll, beforeEach, inject } from "vitest";
2+
import { beforeAll, beforeEach, inject } from "vitest";
33
// hack to keep imported vitest types
44
export type { TestContext } from "vitest";
55
// hack to fix pnpm build issue
@@ -18,16 +18,14 @@ let connection: Connection;
1818
beforeAll(async () => {
1919
const uri = inject("MONGO_URI");
2020
connection = await createConnection(uri).asPromise();
21-
});
22-
23-
afterAll(async () => {
24-
await connection.close();
21+
return async () => {
22+
await connection.close();
23+
};
2524
});
2625

2726
beforeEach(async (context) => {
2827
context.connection = connection.useDb(randomUUID());
29-
});
30-
31-
afterEach(async (context) => {
32-
await context.connection.db?.dropDatabase();
28+
return async () => {
29+
await context.connection.db?.dropDatabase();
30+
};
3331
});

0 commit comments

Comments
 (0)