Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vitest-mms

danielpza20.6kMIT0.2.6TypeScript support: included

mongodb-memory-server integration for vitest

test, vite, vitest, mock, mongodb, mongoose, mongodb-memory-server

readme

vitest-mms NPM Version

mongodb-memory-server integration for vitest

  • mongodb driver support.
  • mongoose support.
  • clear database between each test
  • ootb ready to start writting tests

If you need support for other ORMs, please open an issue or a pull request. See ./tests for more examples.

[!TIP] You can also connect to the mongodb memory server directly by using a connection uri const connectionUri = inject("MONGO_URI");

Installation

npm install -D vitest-mms mongodb-memory-server
yarn add -D vitest-mms mongodb-memory-server
pnpm add -D vitest-mms mongodb-memory-server

Usage with mongodb

[!IMPORTANT] You need to install mongodb separately.

To make it available in the global context for every test you need to add a globalSetup and setupFile in your vitest config:

vitest.config.mjs:

import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    globalSetup: ["vitest-mms/globalSetup"],
    setupFiles: ["vitest-mms/mongodb/setupFile"],
    vitestMms: {
      // optional configuration
      mongodbMemoryServer: {
        // these options are passed to MongoMemoryServer.create(), see https://typegoose.github.io/mongodb-memory-server/docs/guides/quick-start-guide#normal-server
      },
    },
  },
});

tsconfig.json:

{
  "compilerOptions": {
    "types": ["vitest-mms/mongodb/setupFile"]
  }
}

index.test.js:

import { test } from "vitest";

test("my test", async ({ db, mongoClient }) => {
  const users = db.collection("users");
  users.insertOne({ name: "John" });
  expect(await users.countDocuments()).toBe(1);
});
  • mongoClient is the connected MongoClient instance (see import("mongodb").MongoClient)
  • db is a random database name connected to the mongodb-memory-server instance (see import("mongodb").Db)

Usage with mongoose

[!IMPORTANT] You need to install mongoose separately.

vitest.config.mjs:

import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    globalSetup: ["vitest-mms/globalSetup"],
    setupFile: ["vitest-mms/mongoose/setupFile"],
    vitestMms: {
      // optional configuration
      mongodbMemoryServerOptions: {
        // these options are passed to MongoMemoryServer.create(), see https://typegoose.github.io/mongodb-memory-server/docs/guides/quick-start-guide#normal-server
      },
    },
  },
});

tsconfig.json:

{
  "compilerOptions": {
    "types": ["vitest-mms/mongoose/setupFile"]
  }
}

index.test.js:

test("my test", async ({ connection }) => {
  const User = connection.model("User", new Schema({ name: String }));
  await User.create({ name: "John" });
  expect(await User.countDocuments()).toBe(1);
});

Using ReplSet

See https://typegoose.github.io/mongodb-memory-server/docs/guides/quick-start-guide#replicaset

vitest.config.mjs:

import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    globalSetup: ["vitest-mms/globalSetupReplSet"],
    setupFiles: ["vitest-mms/mongodb/setupFile"],
    vitestMms: {
      mongodbMemoryServerOptions: {
        replSet: { count: 4 },
      },
    },
  },
});

Alternative using a extended test context

If you want to avoid the overhead of vitest-mms on every test and instead just want to use it for a subset of your tests, you can use vitest-mms/*/test instead:

vitest.config.mjs:

import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    globalSetup: ["vitest-mms/globalSetup"],
  },
});

index.test.js:

// using the extended test context
import { mssTest } from "vitest-mms/mongodb/test";
// or import { mssTest } from "vitest-mms/mongoose/test";

mssTest("my test", async ({ db, mongoClient }) => {
  const users = db.collection("users");
  users.insertOne({ name: "John" });
  expect(await users.countDocuments()).toBe(1);
});

See https://vitest.dev/guide/test-context.html#extend-test-context for more information

changelog

vitest-mms

v0.2.6

compare changes

💅 Refactors

  • BeforeEach beforeAll return cleanup hook (1289116)

❤️ Contributors

v0.2.5

compare changes

📖 Documentation

❤️ Contributors

v0.2.4

compare changes

🚀 Enhancements

  • Add new entrypoint for mongodb ReplicaSet (374de60)

❤️ Contributors

v0.2.3

compare changes

🚀 Enhancements

  • Allow configuring through test.vitestMms property (#8)

❤️ Contributors

v0.2.2

compare changes

💅 Refactors

📖 Documentation

  • Fix breaking changes section for v0.2.0 version (fed59e0)

🏡 Chore

❤️ Contributors

v0.2.1

compare changes

🚀 Enhancements

  • Add vitest-mms/mongoose/test export (d595648)

🔥 Performance

  • mongodb: Fix mongodb extended test context performance issue (dd1f4cd)

🏡 Chore

  • Ignore CHANGELOG.md formatting (656ec89)
  • Use vitest workspaces for testing (032f975)

✅ Tests

  • mongodb: Fix performance test for mongodb-extended-test (2e9e54c)
  • Update benchmark tests (a367782)

❤️ Contributors

v0.2.0

compare changes

🏡 Chore

  • ⚠️ Remove deprecated exports (80ced6b)
  • ⚠️ mongoose Provide connection instead of mongoose singleton (690b908)

⚠️ Breaking Changes

  • ⚠️ Remove deprecated exports (80ced6b)
  • ⚠️ mongoose Provide connection instead of mongoose singleton (690b908)

❤️ Contributors

v0.1.12

compare changes

🔥 Performance

  • Fix mongoose setupFile performance issue (b6ac0d6)
  • Add performance tests for mongodb helpers too (f37709e)

📖 Documentation

  • Update readme (04b9b1c)
  • Update link to mongodb-memory-server website (be2500d)
  • Update readme (cb382f5)
  • Point to examples in README (29d137b)

🏡 Chore

  • Organize tests (6918e85)
  • Fix prettier failing due to recent renames (77ebe14)

❤️ Contributors

v0.1.11

compare changes

🩹 Fixes

  • Mark mongodb and mongoose as optional (86b14d7)

💅 Refactors

  • Remove unused exported symbol (8cc8b9d)

📖 Documentation

❤️ Contributors

v0.1.10

compare changes

🚀 Enhancements

  • Move mongodb exports to vitest-mms/mongodb/* (ba3417e)

🏡 Chore

  • Fix vitest commonjs warning in tests (64fc3f5)

❤️ Contributors

v0.1.9

compare changes

🩹 Fixes

  • Drop database after each test (b886a67)
  • Drop database after each test in mmsTest (05fa1f4)

📖 Documentation

❤️ Contributors

v0.1.8

compare changes

🚀 Enhancements

💅 Refactors

  • Use global variable instead of modifying the global context (8cc2eff)

🏡 Chore

❤️ Contributors

v0.1.7

compare changes

🚀 Enhancements

  • Add objectIdEqualityTester helper" (ea919c5)

❤️ Contributors

v0.1.6

compare changes

🚀 Enhancements

  • Add objectIdEqualityTester helper (95794ad)

❤️ Contributors

v0.1.5

compare changes

🩹 Fixes

  • Typescript complaining on commonjs (dd6e3de)

❤️ Contributors

v0.1.4

compare changes

🚀 Enhancements

  • Add cjs support (d070ff1)
  • Alternative way to consume context (#7)

🩹 Fixes

  • Export globalSetup types (cfd14cd)

💅 Refactors

  • Better types for globalSetup (4a1f7c2)

📖 Documentation

🏡 Chore

✅ Tests

  • Add mongodb as dependency for the tests (0858f56)

🤖 CI

❤️ Contributors

v0.1.3

compare changes

🩹 Fixes

  • Fix mongo deprecation warnings (95fe353)

📖 Documentation

  • Update npm version badge to link to npm (4c9313f)

🏡 Chore

  • Update dependencies (c037a77)
  • Update dependencies (e556ff9)
  • Update package.json keywords (3f2c5ca)
  • Fix package.json (67401d2)
  • Add typescript support (#6)
  • Fix package.json author (1379f3d)
  • Build before publishing (3b673b9)

🤖 CI

  • Add pnpm cache (72a8d1c)
  • Add tests on CI (#5)

❤️ Contributors

v0.1.2

compare changes

📖 Documentation

  • Add npm badge to readme (229b338)

🏡 Chore

  • deps-dev: Bump vite from 5.0.11 to 5.3.5 in /tests/test-unimport (#2)
  • Use changelogen (e0d729b)
  • Remove changeset config (cd8fb75)

🤖 CI

  • Fix publish script with pnpm (823b9ed)

❤️ Contributors

0.1.1

Patch Changes

  • 80e980f: Adjust package.json fields