Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongodb-memory-server

Package Overview
Dependencies
Maintainers
1
Versions
346
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongodb-memory-server

MongoDB Server for testing (auto-download latest version). The server will allow you to connect your favourite ODM or client library to the MongoDB Server and run parallel integration tests isolated from each other.

  • 6.9.0-beta.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
588K
increased by10.54%
Maintainers
1
Weekly downloads
 
Created

What is mongodb-memory-server?

The mongodb-memory-server package provides an in-memory MongoDB server for testing purposes. It allows developers to run MongoDB instances without needing to install MongoDB on their local machine, making it ideal for unit tests and CI environments.

What are mongodb-memory-server's main functionalities?

Start an in-memory MongoDB server

This code demonstrates how to start an in-memory MongoDB server using mongodb-memory-server and connect to it using Mongoose. The server runs entirely in memory, making it fast and ideal for testing.

const { MongoMemoryServer } = require('mongodb-memory-server');
const mongoose = require('mongoose');

(async () => {
  const mongod = new MongoMemoryServer();
  const uri = await mongod.getUri();

  await mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
  console.log('MongoDB in-memory server started');

  // Your code here

  await mongoose.disconnect();
  await mongod.stop();
})();

Use with Jest for testing

This code shows how to integrate mongodb-memory-server with Jest for testing. It sets up the in-memory MongoDB server before all tests and tears it down after all tests, ensuring a clean state for each test run.

const { MongoMemoryServer } = require('mongodb-memory-server');
const mongoose = require('mongoose');

let mongod;

beforeAll(async () => {
  mongod = new MongoMemoryServer();
  const uri = await mongod.getUri();
  await mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
});

afterAll(async () => {
  await mongoose.disconnect();
  await mongod.stop();
});

// Your tests here

Create and use collections

This code demonstrates how to create and use collections with mongodb-memory-server. It defines a Mongoose model, saves a document, and retrieves all documents from the collection.

const { MongoMemoryServer } = require('mongodb-memory-server');
const mongoose = require('mongoose');

(async () => {
  const mongod = new MongoMemoryServer();
  const uri = await mongod.getUri();

  await mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });

  const Cat = mongoose.model('Cat', { name: String });
  const kitty = new Cat({ name: 'Zildjian' });
  await kitty.save();
  console.log('Cat saved:', kitty);

  const cats = await Cat.find();
  console.log('All cats:', cats);

  await mongoose.disconnect();
  await mongod.stop();
})();

Other packages similar to mongodb-memory-server

Keywords

FAQs

Package last updated on 30 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc