npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

vitest-mms

v0.2.7

Published

mongodb-memory-server integration for vitest

Readme

vitest-mms

NPM Version

mongodb-memory-server integration for vitest

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

General Usage

// tsconfig.json
{
  "compilerOptions": {
    "types": ["vitest-mms/globalSetup"]
  }
}
// vitest.config.mjs
import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    globalSetup: ["vitest-mms/globalSetup"],
  },
});
// index.test.js
import { inject, test } from "vitest";

const MONGO_URI = inject("MONGO_URI");

test("some test", () => {
  // use mongodb/mongoose/other packages to connect to mongodb using MONGO_URI
});

Usage with mongodb

[!IMPORTANT] You need to install mongodb separately.

Using setup test helper

// index.test.js
import { setupDb } from "vitest-mms/mongodb/helpers";

// db is cleared after each test
// mongoClient is disconnected after all tests are done
const { db, mongoClient } = setupDb();

test("some test", async () => {
  // rest of the test
});

Manual Setup

// index.test.js
import { inject, test } from "vitest";
import { MongoClient } from "mongodb";

const MONGO_URI = inject("MONGO_URI");
const mongoClient = new MongoClient(MONGO_URI);

beforeAll(async () => {
  await mongoClient.connect();
  return () => mongoClient.disconnect();
});

test("some test", async () => {
  const db = mongoClient.db("my-db");
  // rest of the test
});

Using extended context

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

// index.test.js
import { mssTest } from "vitest-mms/mongodb/test";

mssTest("another test", ({ db, mongoClient }) => {
  // rest of the test
});
  • db is cleared after each test

Usage with mongoose

[!IMPORTANT] You need to install mongoose separately.

Using setup test helper (mongoose)

// index.test.js
import { test } from "vitest";
import { setupMongooseConnection } from "vitest-mms/mongoose/helpers";

// provides default db connection
// db will be dropped after each test
// connection will be closed after all tests
const { connection } = setupMongooseConnection();

test("some test", async () => {
  // rest of the test
});

Manual Setup (mongoose)

// index.test.js
import { inject, test } from "vitest";
import { createConnection } from "mongoose";

const MONGO_URI = inject("MONGO_URI");
let connection;

beforeAll(async () => {
  connection = await createConnection(MONGO_URI).asPromise();
  return () => connection.close();
});

test("some test", async () => {
  const dbConnection = connection.useDb("my-db");
  // rest of the test
});

Using extended context (mongoose)

import { mssTest } from "vitest-mms/mongoose/test";

// 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);
});
  • connection is the Connection instance returned by mongoose.createConnection scoped to a db. See https://mongoosejs.com/docs/api/connection.html

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"],
    vitestMms: {
      mongodbMemoryServerOptions: {
        replSet: { count: 4 },
      },
    },
  },
});

Legacy setup files

[!WARNING] Although convenient, these helpers have been deprecated since they rely on vitest beforeEach/afterEach hooks which are marked as deprecated by vitest

// vitest.config.mjs
import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    globalSetup: ["vitest-mms/globalSetup"],
    setupFiles: ["vitest-mms/mongodb/setupFile"], // or vitest-mms/mongoose/setupFile
  },
});
// tsconfig.json
{
  "compilerOptions": {
    "types": [
      "vitest-mms/globalSetup",
      // or "vitest-mms/mongoose/setupFile"
      "vitest-mms/mongodb/setupFile"
    ]
  }
}
import { test, expect } from "vitest";

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

// or with mongoose
// test("my test", async ({connection }) => { ... })
  • database is cleared before each test