mongodb-memory-server integration for vitest
npm install -D vitest-mms mongodb-memory-server
yarn add -D vitest-mms mongodb-memory-server
pnpm add -D vitest-mms mongodb-memory-server
// 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
});
Important
You need to install mongodb
separately.
// 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
});
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
Important
You need to install mongoose
separately.
// 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
});
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 theConnection
instance returned bymongoose.createConnection
scoped to a db. See https://mongoosejs.com/docs/api/connection.html
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 },
},
},
},
});
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