UNPKG

vitest-mms

Version:

mongodb-memory-server integration for vitest

28 lines (25 loc) 603 B
import { randomUUID } from 'node:crypto'; import { MongoClient } from 'mongodb'; import { afterAll, test, inject } from 'vitest'; let client; afterAll(async () => { if (client) { await client.close(); } }); const mmsTest = test.extend({ mongoClient: async ({}, use) => { if (!client) { const uri = inject("MONGO_URI"); client = new MongoClient(uri); await client.connect(); } await use(client); }, db: async ({ mongoClient }, use) => { const db = mongoClient.db(randomUUID()); await use(db); await db.dropDatabase(); } }); export { mmsTest };