Skip to content

Commit

Permalink
cleanup collections before all tests instead of after each of them (fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VenomAV committed Oct 18, 2024
1 parent 3b2b87c commit c309999
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/effect-mongodb/test/support/describe-mongo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Effect from "effect/Effect"
import type { Db } from "mongodb"
import { MongoClient } from "mongodb"
import { afterAll, beforeAll, beforeEach, describe, inject } from "vitest"
import { afterAll, beforeAll, describe, inject } from "vitest"

type MongoContext = {
client: Effect.Effect<MongoClient>
Expand All @@ -10,22 +10,25 @@ type MongoContext = {
_database: () => Db
}

export const describeMongo = (suiteName: string, tests: (ctx: MongoContext) => void) => {
describe.sequential(suiteName, () => {
export const describeMongo = (
suiteName: string,
tests: (ctx: MongoContext) => void
) => {
describe(suiteName, () => {
let client: MongoClient
let database: Db
let databaseName: string

beforeAll(async () => {
client = new MongoClient(inject("mongoConnectionString"), { directConnection: true })
client = new MongoClient(inject("mongoConnectionString"), {
directConnection: true
})
await client.connect()

// https://www.mongodb.com/docs/v5.2/reference/limits/#mongodb-limit-Database-Name-Case-Sensitivity
databaseName = suiteName.replace(/[/\\. "$*<>:|?]/g, "-")
database = client.db(databaseName)
})

beforeEach(async () => {
const collections = await database.collections()
await Promise.all(collections.map((x) => x.deleteMany({})))
})
Expand Down

0 comments on commit c309999

Please sign in to comment.