mongodb-memory-server integration for vitest
npm install vitest-mms
mongodb-memory-server integration for vitest
- vitest-mms
- Installation
- General Usage
- Usage with mongodb
- Using setup test helper
- Manual Setup
- Using extended context
- Usage with mongoose
- Using setup test helper (mongoose)
- Manual Setup (mongoose)
- Using extended context (mongoose)
- Using ReplSet
- Legacy setup files
``shell`
npm install -D vitest-mms mongodb-memory-server
yarn add -D vitest-mms mongodb-memory-server
pnpm add -D vitest-mms mongodb-memory-server
`json`
// tsconfig.json
{
"compilerOptions": {
"types": ["vitest-mms/globalSetup"]
}
}
`js
// vitest.config.mjs
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
globalSetup: ["vitest-mms/globalSetup"],
},
});
`
`js
// 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.
`js
// 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
});
`
`js
// 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:
`js
// 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.
`js
// 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
});
`
`js
// 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
});
`
`js
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
See https://typegoose.github.io/mongodb-memory-server/docs/guides/quick-start-guide#replicaset
`js
// 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
`js
// 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
},
});
`
`json`
// tsconfig.json
{
"compilerOptions": {
"types": [
"vitest-mms/globalSetup",
// or "vitest-mms/mongoose/setupFile"
"vitest-mms/mongodb/setupFile"
]
}
}
`js
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