From c13aae99f5a8698c034edd1c35d0cec2bac67c44 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Sun, 20 Jul 2025 00:11:15 -0700 Subject: [PATCH 1/4] docs(guides): add vitest integration example --- .../integration-examples/test-runners.md | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/docs/guides/integration-examples/test-runners.md b/docs/guides/integration-examples/test-runners.md index 2f1bddf70..4dd9db9e5 100644 --- a/docs/guides/integration-examples/test-runners.md +++ b/docs/guides/integration-examples/test-runners.md @@ -142,3 +142,66 @@ For AVA there is a [detailed written tutorial](https://github.com/zellwk/ava/blo :::note Note that this tutorial is pre mongodb-memory-server 7.x. ::: + +## vitest + +For [vitest](https://vitest.dev/), create a [global setup file](https://vitest.dev/config/#globalsetup). + +`vitest.config.mts`: + +```ts +import { defineConfig } from 'vitest/config'; + +export default defineConfig({ + test: { + globalSetup: ['./globalSetup.ts'], + }, +}); +``` + +`globalSetup.ts`: + +```ts +import type { TestProject } from 'vitest/node'; + +declare module 'vitest' { + export interface ProvidedContext { + MONGO_URI: string; + } +} + +export default async function setup({ provide }: TestProject) { + const mongod = await MongoMemoryServer.create(); + + const uri = mongod.getUri(); + + provide('MONGO_URI', uri); + + return async () => { + await mongod.stop(); + }; +} +``` + +Then use it in your tests: + +`example.test.js` + +```ts +import { inject, test } from 'vitest'; +import { MongoClient } from 'mongodb'; + +const MONGO_URI = inject('MONGO_URI'); +const mongoClient = new MongoClient(MONGO_URI); + +beforeAll(async () => { + await mongoClient.connect(); + return () => mongoClient.disconnect(); +}); + +test('...', () => { + const db = mongoClient.db('my-db'); +}); +``` + +See also [vitest-mms](https://github.com/danielpza/vitest-mms) From 0ff2cfe96c4ae471b545c81607db905f0dd62e8c Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Thu, 24 Jul 2025 23:23:12 -0700 Subject: [PATCH 2/4] update based on code review --- docs/guides/integration-examples/test-runners.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/guides/integration-examples/test-runners.md b/docs/guides/integration-examples/test-runners.md index 4dd9db9e5..c49d25cf8 100644 --- a/docs/guides/integration-examples/test-runners.md +++ b/docs/guides/integration-examples/test-runners.md @@ -145,6 +145,8 @@ Note that this tutorial is pre mongodb-memory-server 7.x. ## vitest +vitest version 3 + For [vitest](https://vitest.dev/), create a [global setup file](https://vitest.dev/config/#globalsetup). `vitest.config.mts`: @@ -204,4 +206,14 @@ test('...', () => { }); ``` -See also [vitest-mms](https://github.com/danielpza/vitest-mms) +See also [vitest-mms](https://github.com/danielpza/vitest-mms), which provides the `globalSetup` configuration among others helpers: + +```ts +import { defineConfig } from 'vitest/config'; + +export default defineConfig({ + test: { + globalSetup: ['vitest-mms/globalSetup'], + }, +}); +``` From 5f9762e71c35f65295a07cb3e000aff92717d580 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Thu, 24 Jul 2025 23:25:53 -0700 Subject: [PATCH 3/4] add note --- docs/guides/integration-examples/test-runners.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/guides/integration-examples/test-runners.md b/docs/guides/integration-examples/test-runners.md index c49d25cf8..e7099b2da 100644 --- a/docs/guides/integration-examples/test-runners.md +++ b/docs/guides/integration-examples/test-runners.md @@ -206,6 +206,10 @@ test('...', () => { }); ``` +:::note +Keep in mind that the global setup is running in a different global scope, so your tests don't have access to variables defined here. However, you can pass down serializable data to tests via [provide](https://vitest.dev/config/#provide) method as described above. +::: + See also [vitest-mms](https://github.com/danielpza/vitest-mms), which provides the `globalSetup` configuration among others helpers: ```ts From 0be8be7e83ced803f92074b2c2f303b69bf97173 Mon Sep 17 00:00:00 2001 From: Daniel Perez Alvarez Date: Thu, 24 Jul 2025 23:26:20 -0700 Subject: [PATCH 4/4] add missing import --- docs/guides/integration-examples/test-runners.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/guides/integration-examples/test-runners.md b/docs/guides/integration-examples/test-runners.md index e7099b2da..affe56272 100644 --- a/docs/guides/integration-examples/test-runners.md +++ b/docs/guides/integration-examples/test-runners.md @@ -165,6 +165,7 @@ export default defineConfig({ ```ts import type { TestProject } from 'vitest/node'; +import { MongoMemoryServer } from 'mongodb-memory-server'; declare module 'vitest' { export interface ProvidedContext {