vitest-mongodb
Version:
Run your tests using Vitest and MongoDB in Memory server.
35 lines (29 loc) • 1.27 kB
JavaScript
;
const mongodbMemoryServer = require('mongodb-memory-server');
const debugInit = require('debug');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
const debugInit__default = /*#__PURE__*/_interopDefaultCompat(debugInit);
const debug = debugInit__default(`vitest-jest:${process.env.VITEST_WORKER_ID ?? 0}`);
async function setup(options) {
const type = options?.type ?? "default";
const serverOptions = options?.serverOptions;
debug("Starting setup with options:", { type, serverOptions });
debug("Starting mongo memory server");
if (type !== "replSet") {
globalThis.__MONGO_DB__ = await mongodbMemoryServer.MongoMemoryServer.create(serverOptions);
globalThis.__MONGO_URI__ = globalThis.__MONGO_DB__.getUri();
} else {
globalThis.__MONGO_DB__ = await mongodbMemoryServer.MongoMemoryReplSet.create(serverOptions);
globalThis.__MONGO_URI__ = globalThis.__MONGO_DB__.getUri();
}
debug("Mongo URI:", globalThis.__MONGO_URI__);
}
async function teardown() {
debug("Starting teardown");
if (globalThis.__MONGO_DB__) {
debug("Stopping mongo memory server");
await globalThis.__MONGO_DB__.stop();
}
}
exports.setup = setup;
exports.teardown = teardown;