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-mmsVerified import paths — ran on the pinned version, not inferred.
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.
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.
Install required peer dependencies: `npm install -D mongodb mongodb-memory-server mongoose vitest` or `yarn add -D mongodb mongodb-memory-server mongoose vitest`.
Add `"types": ["vitest-mms/globalSetup", ...]` to your `compilerOptions` in `tsconfig.json`.
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.Run `npm install -D mongodb-memory-server` or `yarn add -D mongodb-memory-server`.
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`).
Ensure `globalSetup: ['vitest-mms/globalSetup']` is correctly specified in your `vitest.config.mjs` and that `mongodb-memory-server` is installed.