Registry / testing / vitest-mms

vitest-mms

JSON →
library0.2.7jsnpmunverified

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.

npm install vitest-mms
INSTALL
IMPORT
SIG · VITEST-MMS
V
vitest-mms
testingjavascriptv0.2.7
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

globalSetup
✓ import 'vitest-mms/globalSetup'
✗ import { globalSetup } from 'vitest-mms'
This is a side-effect import for Vitest's global setup file. Ensure it's listed in vitest.config.mjs and tsconfig.json types.
setupDb
✓ import { setupDb } from 'vitest-mms/mongodb/helpers'
Use this helper for simplified setup with the native MongoDB driver within individual test files.
mssTest
✓ import { mssTest } from 'vitest-mms/mongodb/test'
✗ import { mssTest } from 'vitest-mms/mongoose/test'
This `mssTest` variant extends Vitest's context with `db` and `mongoClient` for the native MongoDB driver. A separate `mssTest` exists for Mongoose at a different path.
setupMongooseConnection
✓ import { setupMongooseConnection } from 'vitest-mms/mongoose/helpers'
Use this helper for simplified setup with Mongoose within individual test files.

Demonstrates the global Vitest setup using `vitest-mms/globalSetup` and how to inject the `MONGO_URI` into tests for manual connection with the native MongoDB driver.

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(); });
Debug
Known issues
breakingVersion 0.2.0 introduced breaking changes by removing deprecated exports. Code relying on older, deprecated paths or symbols will no longer function.
fix
Refer to the updated documentation and release notes for version 0.2.0 to update import paths and symbols. Ensure all exports are sourced from current `vitest-mms/mongodb/*` or `vitest-mms/mongoose/*` paths.
affects: >=0.2.0
gotchaAll database-related dependencies (mongodb, mongodb-memory-server, mongoose, vitest) are peer dependencies and must be installed explicitly in your project.
fix
Install required peer dependencies: `npm install -D mongodb mongodb-memory-server mongoose vitest` or `yarn add -D mongodb mongodb-memory-server mongoose vitest`.
affects: >=0.1.0
gotchaWhen using TypeScript with `globalSetup`, you must add `'vitest-mms/globalSetup'` to the `types` array in your `tsconfig.json` to correctly extend the Vitest `TestContext`.
fix
Add `"types": ["vitest-mms/globalSetup", ...]` to your `compilerOptions` in `tsconfig.json`.
affects: >=0.1.0
gotchaThe `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`).
fix
Verify the import path matches your usage: `import { mssTest } from 'vitest-mms/mongodb/test'` for native driver or `import { mssTest } from 'vitest-mms/mongoose/test'` for Mongoose.
affects: >=0.2.0
Errors
Common errors & fixes
Cannot find module 'mongodb-memory-server' or its corresponding type declarations.
The `mongodb-memory-server` package is a peer dependency and has not been explicitly installed in the project.
fix
Run `npm install -D mongodb-memory-server` or `yarn add -D mongodb-memory-server`.
Property 'db' does not exist on type 'TestContext'.
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`.
fix
Add `'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
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.
fix
Ensure `globalSetup: ['vitest-mms/globalSetup']` is correctly specified in your `vitest.config.mjs` and that `mongodb-memory-server` is installed.
Upgrade
Version history
0.2.7latest on npm
Audit
Dependencies
mongodboptionalRequired for MongoDB native driver integration. Peer dependency.
mongodb-memory-serverrequiredCore dependency for providing an in-memory MongoDB instance. Peer dependency.
mongooseoptionalRequired for Mongoose ODM integration. Peer dependency.
vitestrequiredThe primary testing framework this library integrates with. Peer dependency.
Agent activity
21 hits · last 30 days
node
18
OpenAI (training)
1
Resources
vitest-mms — npm install vitest-mms · libregistry