Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions docs/guides/integration-examples/test-runners.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)