{"id":16644581,"url":"https://github.com/danielpza/vitest-mms","last_synced_at":"2026-05-10T08:13:09.676Z","repository":{"id":215848085,"uuid":"739893989","full_name":"danielpza/vitest-mms","owner":"danielpza","description":"mongodb-memory-server integration for vitest","archived":false,"fork":false,"pushed_at":"2025-01-18T19:29:12.000Z","size":305,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-15T22:48:11.870Z","etag":null,"topics":["mock","mongodb","mongodb-memory-server","mongoose","test","vitest"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danielpza.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"danielpza"}},"created_at":"2024-01-06T21:36:15.000Z","updated_at":"2025-03-04T01:58:29.000Z","dependencies_parsed_at":"2024-08-08T02:05:07.921Z","dependency_job_id":"6be0ce2d-fde0-431e-b187-5e3fcb39326c","html_url":"https://github.com/danielpza/vitest-mms","commit_stats":null,"previous_names":["danielpza/vitest-mms"],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielpza%2Fvitest-mms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielpza%2Fvitest-mms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielpza%2Fvitest-mms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielpza%2Fvitest-mms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielpza","download_url":"https://codeload.github.com/danielpza/vitest-mms/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244822756,"owners_count":20516160,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mock","mongodb","mongodb-memory-server","mongoose","test","vitest"],"created_at":"2024-10-12T08:11:46.040Z","updated_at":"2026-05-10T08:13:09.669Z","avatar_url":"https://github.com/danielpza.png","language":"TypeScript","funding_links":["https://github.com/sponsors/danielpza"],"categories":["Plugins"],"sub_categories":["Templates"],"readme":"# vitest-mms\n\n[![NPM Version](https://img.shields.io/npm/v/vitest-mms)](https://www.npmjs.com/package/vitest-mms)\n\n[mongodb-memory-server](https://typegoose.github.io/mongodb-memory-server/) plugin for [vitest](https://vitest.dev/)\n\n## Installation\n\n```shell\nnpm install -D vitest-mms mongodb-memory-server\nyarn add -D vitest-mms mongodb-memory-server\npnpm add -D vitest-mms mongodb-memory-server\n```\n\n## General Usage\n\n### Setup\n\nThe main entrypoint is the `vitestMms` plugin. It extends the vitest context with the `MONGO_URI` value, which you can import with `inject`:\n\n```js\n// vitest.config.mjs\nimport { defineConfig } from \"vitest/config\";\nimport vitestMms from \"vitest-mms\";\n\nexport default defineConfig({\n  plugins: [\n    vitestMms({\n      mongodbMemoryServerOptions: {\n        /* optional mongodb-memory-server options */\n      },\n    }),\n  ],\n});\n```\n\nThen in your tests, use `inject(\"MONGO_URI\")` to get the started server\n\n```js\n// index.test.js\nimport { inject } from \"vitest\";\n\nconst MONGO_URI = inject(\"MONGO_URI\");\n\n// use mongodb/mongoose/other packages to connect to mongodb using MONGO_URI\nimport { MongoClient } from \"mongodb\";\n\nconst mongoClient = new MongoClient(MONGO_URI);\n```\n\n### MongoDB Example\n\n```js\n// index.test.js\nimport { inject, test } from \"vitest\";\nimport { MongoClient } from \"mongodb\";\n\nconst MONGO_URI = inject(\"MONGO_URI\");\nconst mongoClient = new MongoClient(MONGO_URI);\n\nbeforeAll(async () =\u003e {\n  await mongoClient.connect();\n  return () =\u003e mongoClient.disconnect();\n});\n\ntest(\"some test\", async () =\u003e {\n  const db = mongoClient.db(\"my-db\");\n  // rest of the test\n});\n```\n\n### Mongoose Example\n\n```js\n// index.test.js\nimport { inject, test } from \"vitest\";\nimport { createConnection } from \"mongoose\";\n\nconst MONGO_URI = inject(\"MONGO_URI\");\nlet connection;\n\nbeforeAll(async () =\u003e {\n  connection = await createConnection(MONGO_URI).asPromise();\n  return () =\u003e connection.close();\n});\n\ntest(\"some test\", async () =\u003e {\n  const dbConnection = connection.useDb(\"my-db\");\n  // rest of the test\n});\n```\n\n## Additional helpers\n\nThis plugin exports additional entrypoints to help setup tests and cleanup. These are optional.\n\n### Mongodb\n\n\u003e [!IMPORTANT]\n\u003e You need to install `mongodb` separately.\n\n#### Using setup test helper\n\n```js\n// index.test.js\nimport { setupDb } from \"vitest-mms/mongodb/helpers\";\n\n// db is cleared after each test\n// mongoClient is disconnected after all tests are done\nconst { db, mongoClient } = setupDb();\n\ntest(\"some test\", async () =\u003e {\n  // rest of the test\n});\n```\n\n#### Using extended context\n\nSee https://vitest.dev/guide/test-context.html#extend-test-context:\n\n```js\n// index.test.js\nimport { mssTest } from \"vitest-mms/mongodb/test\";\n\nmssTest(\"another test\", ({ db, mongoClient }) =\u003e {\n  // rest of the test\n});\n```\n\n- db is cleared after each test\n\n### Mongoose\n\n\u003e [!IMPORTANT]\n\u003e You need to install `mongoose` separately.\n\n#### Using setup test helper (mongoose)\n\n```js\n// index.test.js\nimport { test } from \"vitest\";\nimport { setupMongooseConnection } from \"vitest-mms/mongoose/helpers\";\n\n// provides default db connection\n// db will be dropped after each test\n// connection will be closed after all tests\nconst { connection } = setupMongooseConnection();\n\ntest(\"some test\", async () =\u003e {\n  // rest of the test\n});\n```\n\n#### Using extended context (mongoose)\n\n```js\nimport { mssTest } from \"vitest-mms/mongoose/test\";\n\n// index.test.js\ntest(\"my test\", async ({ connection }) =\u003e {\n  const User = connection.model(\"User\", new Schema({ name: String }));\n  await User.create({ name: \"John\" });\n  expect(await User.countDocuments()).toBe(1);\n});\n```\n\n- `connection` is the `Connection` instance returned by `mongoose.createConnection` scoped to a db. See https://mongoosejs.com/docs/api/connection.html\n\n## Using ReplSet\n\nSee https://typegoose.github.io/mongodb-memory-server/docs/guides/quick-start-guide#replicaset\n\n```js\n// vitest.config.mjs\nimport { defineConfig } from \"vitest/config\";\nimport vitestMms from \"vitest-mms\";\n\nexport default defineConfig({\n  plugins: [\n    vitestMms({\n      server: \"replset\",\n      mongodbMemoryServerOptions: {\n        replSet: { count: 4 },\n      },\n    }),\n  ],\n});\n```\n\n## Legacy exports\n\nThe following entrypoint are only available as legacy code and will be removed in future version\n\n### Manual Setup\n\nThis is deprecated in favor of using the plugin\n\n```json\n// tsconfig.json\n{\n  \"compilerOptions\": {\n    \"types\": [\"vitest-mms/globalSetup\"]\n  }\n}\n```\n\n```js\n// vitest.config.mjs\nimport { defineConfig } from \"vitest/config\";\n\nexport default defineConfig({\n  test: {\n    globalSetup: [\"vitest-mms/globalSetup\"],\n  },\n});\n```\n\n### Legacy setup files\n\n\u003e [!WARNING]\n\u003e Although convenient, these helpers have been deprecated since they rely on vitest [beforeEach/afterEach](https://vitest.dev/guide/test-context.html#beforeeach-and-aftereach) hooks which are marked as deprecated by vitest\n\n```js\n// vitest.config.mjs\nimport { defineConfig } from \"vitest/config\";\n\nexport default defineConfig({\n  test: {\n    globalSetup: [\"vitest-mms/globalSetup\"],\n    setupFiles: [\"vitest-mms/mongodb/setupFile\"], // or vitest-mms/mongoose/setupFile\n  },\n});\n```\n\n```json\n// tsconfig.json\n{\n  \"compilerOptions\": {\n    \"types\": [\n      \"vitest-mms/globalSetup\",\n      // or \"vitest-mms/mongoose/setupFile\"\n      \"vitest-mms/mongodb/setupFile\"\n    ]\n  }\n}\n```\n\n```js\nimport { test, expect } from \"vitest\";\n\ntest(\"my test\", async ({ db, mongoClient }) =\u003e {\n  const users = db.collection(\"users\");\n  users.insertOne({ name: \"John\" });\n  expect(await users.countDocuments()).toBe(1);\n});\n\n// or with mongoose\n// test(\"my test\", async ({connection }) =\u003e { ... })\n```\n\n- database is cleared before each test\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielpza%2Fvitest-mms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielpza%2Fvitest-mms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielpza%2Fvitest-mms/lists"}