{"id":16267,"library":"vitest-mms","title":"Vitest MongoDB Memory Server Integration","description":"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.","status":"active","version":"0.2.7","language":"javascript","source_language":"en","source_url":"https://github.com/danielpza/vitest-mms","tags":["javascript","test","vite","vitest","mock","mongodb","mongoose","mongodb-memory-server"],"install":[{"cmd":"npm install vitest-mms","lang":"bash","label":"npm"},{"cmd":"yarn add vitest-mms","lang":"bash","label":"yarn"},{"cmd":"pnpm add vitest-mms","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for MongoDB native driver integration. Peer dependency.","package":"mongodb","optional":true},{"reason":"Core dependency for providing an in-memory MongoDB instance. Peer dependency.","package":"mongodb-memory-server","optional":false},{"reason":"Required for Mongoose ODM integration. Peer dependency.","package":"mongoose","optional":true},{"reason":"The primary testing framework this library integrates with. Peer dependency.","package":"vitest","optional":false}],"imports":[{"note":"This is a side-effect import for Vitest's global setup file. Ensure it's listed in vitest.config.mjs and tsconfig.json types.","wrong":"import { globalSetup } from 'vitest-mms'","symbol":"globalSetup","correct":"import 'vitest-mms/globalSetup'"},{"note":"Use this helper for simplified setup with the native MongoDB driver within individual test files.","symbol":"setupDb","correct":"import { setupDb } from 'vitest-mms/mongodb/helpers'"},{"note":"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.","wrong":"import { mssTest } from 'vitest-mms/mongoose/test'","symbol":"mssTest","correct":"import { mssTest } from 'vitest-mms/mongodb/test'"},{"note":"Use this helper for simplified setup with Mongoose within individual test files.","symbol":"setupMongooseConnection","correct":"import { setupMongooseConnection } from 'vitest-mms/mongoose/helpers'"}],"quickstart":{"code":"import { defineConfig } from 'vitest/config';\nimport { inject, test, expect } from 'vitest';\nimport { MongoClient } from 'mongodb';\n\n// vitest.config.mjs (or .ts)\nexport default defineConfig({\n  test: {\n    globalSetup: ['./vitest.globalSetup.ts'], // or vitest-mms/globalSetup directly\n  },\n});\n\n// vitest.globalSetup.ts\nimport 'vitest-mms/globalSetup';\n\n// index.test.ts\nconst MONGO_URI = inject<string>('MONGO_URI');\n\ntest('should connect to in-memory MongoDB', async () => {\n  expect(MONGO_URI).toBeTypeOf('string');\n  expect(MONGO_URI).toMatch(/^mongodb:\\/\\/127\\.0\\.0\\.1:\\d+\\//);\n\n  const client = new MongoClient(MONGO_URI);\n  await client.connect();\n  const db = client.db('testdb');\n  const collection = db.collection('testcollection');\n  await collection.insertOne({ name: 'Test Document' });\n  const doc = await collection.findOne({ name: 'Test Document' });\n  expect(doc).toBeDefined();\n  await client.close();\n});\n","lang":"typescript","description":"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."},"warnings":[{"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.","message":"Version 0.2.0 introduced breaking changes by removing deprecated exports. Code relying on older, deprecated paths or symbols will no longer function.","severity":"breaking","affected_versions":">=0.2.0"},{"fix":"Install required peer dependencies: `npm install -D mongodb mongodb-memory-server mongoose vitest` or `yarn add -D mongodb mongodb-memory-server mongoose vitest`.","message":"All database-related dependencies (mongodb, mongodb-memory-server, mongoose, vitest) are peer dependencies and must be installed explicitly in your project.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Add `\"types\": [\"vitest-mms/globalSetup\", ...]` to your `compilerOptions` in `tsconfig.json`.","message":"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`.","severity":"gotcha","affected_versions":">=0.1.0"},{"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.","message":"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`).","severity":"gotcha","affected_versions":">=0.2.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Run `npm install -D mongodb-memory-server` or `yarn add -D mongodb-memory-server`.","cause":"The `mongodb-memory-server` package is a peer dependency and has not been explicitly installed in the project.","error":"Cannot find module 'mongodb-memory-server' or its corresponding type declarations."},{"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`).","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`.","error":"Property 'db' does not exist on type 'TestContext'."},{"fix":"Ensure `globalSetup: ['vitest-mms/globalSetup']` is correctly specified in your `vitest.config.mjs` and that `mongodb-memory-server` is installed.","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.","error":"TypeError: Cannot read properties of undefined (reading 'collection') at globalSetup"}],"ecosystem":"npm"}