Vitest MongoDB Memory Server Integration
vitest-mms (vitest-mongodb-memory-server) provides seamless integration of mongodb-memory-server with Vitest for efficient and isolated database testing. It allows developers to spin up an in-memory MongoDB instance for each test run or globally, ensuring a clean state. The library, currently at version 0.2.7, is in active development with frequent minor releases, indicating a rapid iteration cycle. Its key differentiators include pre-configured global setup hooks and dedicated test helpers (`setupDb`, `setupMongooseConnection`) for both the `mongodb` native driver and `mongoose` ODM, significantly reducing boilerplate. It also extends Vitest's test context, providing direct access to the `db` object or `mongoose` connection within test functions, simplifying interaction with the temporary database. This approach helps maintain test isolation and speeds up test execution compared to relying on external database instances.
Common errors
-
Cannot find module 'mongodb-memory-server' or its corresponding type declarations.
cause The `mongodb-memory-server` package is a peer dependency and has not been explicitly installed in the project.fixRun `npm install -D mongodb-memory-server` or `yarn add -D mongodb-memory-server`. -
Property 'db' does not exist on type 'TestContext'.
cause When using `vitest-mms/globalSetup` with TypeScript, the `tsconfig.json` has not been updated to include the type declarations, or you're using a plain `test` instead of `mssTest`.fixAdd `'vitest-mms/globalSetup'` to the `types` array in `compilerOptions` of your `tsconfig.json`. If using extended context, ensure you are importing and using `mssTest` from the correct path (e.g., `vitest-mms/mongodb/test`). -
TypeError: Cannot read properties of undefined (reading 'collection') at globalSetup
cause The global setup file `vitest-mms/globalSetup` was not properly configured in `vitest.config.mjs` or there's an issue with the underlying `mongodb-memory-server` initialization.fixEnsure `globalSetup: ['vitest-mms/globalSetup']` is correctly specified in your `vitest.config.mjs` and that `mongodb-memory-server` is installed.
Warnings
- breaking Version 0.2.0 introduced breaking changes by removing deprecated exports. Code relying on older, deprecated paths or symbols will no longer function.
- gotcha All database-related dependencies (mongodb, mongodb-memory-server, mongoose, vitest) are peer dependencies and must be installed explicitly in your project.
- gotcha When using TypeScript with `globalSetup`, you must add `'vitest-mms/globalSetup'` to the `types` array in your `tsconfig.json` to correctly extend the Vitest `TestContext`.
- gotcha The `mssTest` helper is overloaded; it exists under both `vitest-mms/mongodb/test` and `vitest-mms/mongoose/test`. Ensure you are importing the correct one for your chosen driver/ODM, as their extended contexts differ (e.g., `db` vs. `connection`).
Install
-
npm install vitest-mms -
yarn add vitest-mms -
pnpm add vitest-mms
Imports
- globalSetup
import { globalSetup } from 'vitest-mms'import 'vitest-mms/globalSetup'
- setupDb
import { setupDb } from 'vitest-mms/mongodb/helpers' - mssTest
import { mssTest } from 'vitest-mms/mongoose/test'import { mssTest } from 'vitest-mms/mongodb/test' - setupMongooseConnection
import { setupMongooseConnection } from 'vitest-mms/mongoose/helpers'
Quickstart
import { defineConfig } from 'vitest/config';
import { inject, test, expect } from 'vitest';
import { MongoClient } from 'mongodb';
// vitest.config.mjs (or .ts)
export default defineConfig({
test: {
globalSetup: ['./vitest.globalSetup.ts'], // or vitest-mms/globalSetup directly
},
});
// vitest.globalSetup.ts
import 'vitest-mms/globalSetup';
// index.test.ts
const MONGO_URI = inject<string>('MONGO_URI');
test('should connect to in-memory MongoDB', async () => {
expect(MONGO_URI).toBeTypeOf('string');
expect(MONGO_URI).toMatch(/^mongodb:\/\/127\.0\.0\.1:\d+\//);
const client = new MongoClient(MONGO_URI);
await client.connect();
const db = client.db('testdb');
const collection = db.collection('testcollection');
await collection.insertOne({ name: 'Test Document' });
const doc = await collection.findOne({ name: 'Test Document' });
expect(doc).toBeDefined();
await client.close();
});