Manage & spin up mongodb server binaries with zero(or slight) configuration for tests.
Manage & spin up mongodb server binaries with zero(or slight) configuration for tests.
This package spins up an actual/real MongoDB server programmatically from within nodejs, for testing or mocking during development. By default it holds the data in memory. A fresh spun up mongod process takes about 7Mb of memory. The server will allow you to connect your favorite ODM or client library to the MongoDB server and run integration tests isolated from each other.
On install, this package downloads the latest MongoDB binaries and saves them to a cache folder. (only mongodb-memory-server-core does not download on postinstall)
On starting a new instance of the memory server, if the binary cannot be found, it will be auto-downloaded (if RUNTIME_DOWNLOAD option is truthy), thus the first run may take some time. All further runs will be fast, because they will use the downloaded binaries.
This package automatically downloads binaries from https://fastdl.mongodb.org/ according to your operating system. You can see all available versions for Linux (Ubuntu, RHEL, Debian, SUSE, Amazon), OSX, and Windows.
If your network is behind a proxy, make sure that it is configured through the
HTTPS_PROXYorHTTP_PROXYenvironment variable.
Every MongoMemoryServer instance creates and starts a fresh MongoDB server on some free port. You may start up several mongod simultaneously. When you terminate your script or call stop(), the MongoDB server(s) will be automatically shutdown.
Works perfectly with any CI runner that can run NodeJS applications.
Table of Contents generated with DocToc
This tool provides three packages for different purposes:
mongodb-memory-server, mongodb-memory-server-global-*)mongodb-memory-server-core)Choose any package, because they are the same. They differ only in the default configuration, which you may override (see section Available options).
And one of those (on Linux):
lsb-core installed (or any that provides the lsb_release command)/etc/os-release file that is compliant to the OS-Release Spec/etc/*-release file that is compliant to the OS-Release Spec (and does not include lsb)On Linux, you will also need libcurl4 (or libcurl3 on some older distro versions). This will probably only be an issue on "slim" Docker images.
Choose the right package for the task
The default behavior is that version 8.2.6 for your OS will be downloaded. By setting Environment variables you are able to specify which version and binary will be downloaded:
export MONGOMS_DOWNLOAD_URL=https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.2.8.tgz
export MONGOMS_VERSION=4.2.8
A Normal Server can be easily started with:
import { MongoMemoryServer } from 'mongodb-memory-server';
// This will create an new instance of "MongoMemoryServer" and automatically start it
const mongod = await MongoMemoryServer.create();
const uri = mongod.getUri();
// The Server can be stopped again with
await mongod.stop();
All options with ? are optional, some options upon specified are required.
…
A ReplicaSet can be easily started with:
import { MongoMemoryReplSet } from 'mongodb-memory-server';
// This will create an new instance of "MongoMemoryReplSet" and automatically start all Servers
const replset = await MongoMemoryReplSet.create({ replSet: { count: 4 } }); // This will create an ReplSet with 4 members
const uri = replset.getUri();
// The ReplSet can be stopped again with
await replset.stop();
All options are optional.
…
Documentation of Config Options
A example test file for a single MongoMemoryServer within jest.
A example test file for multiple MongoMemoryServer within jest.
A example test file for a single MongoMemoryReplSet within jest.
// assuming [email protected]
import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
const mongoServer = await MongoMemoryServer.create();
(async () => {
await mongoose.connect(mongoServer.getUri(), { dbName: "verifyMASTER" });
// your code here
await mongoose.disconnect();
})();
Documentation for Test Runner Integration Examples
There isn't currently an official MongoDB release for alpine linux. This means that we can't pull binaries for Alpine
(or any other platform that isn't officially supported by MongoDB), but you can use a Docker image that already has mongod
built in and then set the MONGOMS_SYSTEM_BINARY variable to point at that binary. This should allow you to use mongodb-memory-server on any system on which you can install mongod manually.
The Debug mode can be enabled with an Environment-Variable or in the package.json "config" section:
MONGOMS_DEBUG=1 # also available case-insensitive values: "on" "yes" "true"
or
{
"config": {
"mongodbMemoryServer": {
"debug": "1" // also available case-insensitive values: "on" "yes" "true"
}
}
}
Also see the Enable Debug Mode Guide.
Contributing Guidelines are setup in CONTRIBUTING
To ask questions or just talk with us, join our Discord Server.
Inspired by alternative runners for mongodb-prebuilt:
This project exists thanks to all the people who contribute.
Thank you to all our backers! [Become a backer]
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
MIT
No open issues yet, or sync has not completed.