Skip to content

docs(guides): add vitest integration example #937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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).
Comment on lines +146 to +148
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a badge similar to jest and mocha:

<span class="badge badge--secondary">jest version 29</span>


`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';
Copy link
Member

@hasezoey hasezoey Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import for mongodb-memory-server missing?


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)
Copy link
Member

@hasezoey hasezoey Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of just a simple see also, could you add a simple explanation on why this should be a see also, like a little description of what this package is doing / can be used for?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a note similar to jests tests and global setup do not share a environment:

:::note
Keep in mind that jest's global-setup and global-teardown do **not** share a environment with the tests themself, and so require `setupFile` / `setupFilesAfterEnv` to actually connect.
:::

re:

Beware 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 method:
Source: the warning