{"id":13527595,"url":"https://github.com/typegoose/mongodb-memory-server","last_synced_at":"2025-05-13T17:04:11.732Z","repository":{"id":37734270,"uuid":"91011557","full_name":"typegoose/mongodb-memory-server","owner":"typegoose","description":"Manage \u0026 spin up mongodb server binaries with zero(or slight) configuration for tests.","archived":false,"fork":false,"pushed_at":"2025-04-22T12:34:49.000Z","size":19215,"stargazers_count":2688,"open_issues_count":18,"forks_count":185,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-05-05T22:55:58.927Z","etag":null,"topics":["mock","mongodb","mongoose","testing","testing-tools","tests"],"latest_commit_sha":null,"homepage":"https://typegoose.github.io/mongodb-memory-server/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/typegoose.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":null,"patreon":null,"open_collective":"mongodb-memory-server","ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2017-05-11T18:32:46.000Z","updated_at":"2025-05-03T09:40:48.000Z","dependencies_parsed_at":"2023-10-10T13:32:56.000Z","dependency_job_id":"691228ba-e2e3-4e4b-a3ed-49c80ac059ed","html_url":"https://github.com/typegoose/mongodb-memory-server","commit_stats":{"total_commits":2448,"total_committers":114,"mean_commits":"21.473684210526315","dds":"0.29289215686274506","last_synced_commit":"8de148d5810ae4ad1c237081d9a49a255640bc35"},"previous_names":["typegoose/mongodb-memory-server"],"tags_count":353,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typegoose%2Fmongodb-memory-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typegoose%2Fmongodb-memory-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typegoose%2Fmongodb-memory-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typegoose%2Fmongodb-memory-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/typegoose","download_url":"https://codeload.github.com/typegoose/mongodb-memory-server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252590551,"owners_count":21772936,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mock","mongodb","mongoose","testing","testing-tools","tests"],"created_at":"2024-08-01T06:01:53.291Z","updated_at":"2025-05-05T22:56:37.044Z","avatar_url":"https://github.com/typegoose.png","language":"TypeScript","readme":"# MongoDB In-Memory Server\n\n[![Node.js CI](https://github.com/typegoose/mongodb-memory-server/workflows/Node.js%20CI/badge.svg)](https://github.com/typegoose/mongodb-memory-server/actions/workflows/tests.yml?query=workflow%3A%22Node.js+CI%22)\n[![NPM version](https://img.shields.io/npm/v/mongodb-memory-server.svg)](https://www.npmjs.com/package/mongodb-memory-server)\n[![Downloads stat](https://img.shields.io/npm/dt/mongodb-memory-server.svg)](http://www.npmtrends.com/mongodb-memory-server)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n[![TypeScript compatible](https://img.shields.io/badge/typescript-compatible-brightgreen.svg)](https://www.typescriptlang.org)\n[![codecov.io](https://codecov.io/github/typegoose/mongodb-memory-server/coverage.svg?branch=master)](https://codecov.io/github/typegoose/mongodb-memory-server?branch=master)\n[![Backers on Open Collective](https://opencollective.com/mongodb-memory-server/backers/badge.svg)](#backers)\n[![Sponsors on Open Collective](https://opencollective.com/mongodb-memory-server/sponsors/badge.svg)](#sponsors)\n[![mongodb-memory-server-core](https://snyk.io/advisor/npm-package/mongodb-memory-server-core/badge.svg)](https://snyk.io/advisor/npm-package/mongodb-memory-server-core)\n\nThis 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.\n\nOn install, this [package downloads](#configuring-which-mongod-binary-to-use) the latest MongoDB binaries and saves them to a cache folder. (only `mongodb-memory-server-core` does not download on `postinstall`)\n\nOn starting a new instance of the memory server, if the binary cannot be found, it will be auto-downloaded (if [`RUNTIME_DOWNLOAD`](https://typegoose.github.io/mongodb-memory-server/docs/api/config-options#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.\n\nThis package automatically downloads binaries from [https://fastdl.mongodb.org/](https://fastdl.mongodb.org/) according to your operating system. You can see all available versions for [Linux](https://www.mongodb.org/dl/linux) (Ubuntu, RHEL, Debian, SUSE, Amazon), [OSX](https://www.mongodb.org/dl/osx), and [Windows](https://www.mongodb.org/dl/win32).\n\n\u003e If your network is behind a proxy, make sure that it is configured through the `HTTPS_PROXY` or `HTTP_PROXY` environment variable.\n\nEvery `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.\n\nWorks perfectly with any CI runner that can run NodeJS applications.\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n**Table of Contents**  *generated with [DocToc](https://github.com/thlorenz/doctoc)*\n\n- [Installation](#installation)\n  - [Requirements](#requirements)\n  - [Choose the Correct Package](#choose-the-correct-package)\n  - [Configuring which mongod binary to use](#configuring-which-mongod-binary-to-use)\n- [Usage](#usage)\n  - [Simple server start](#simple-server-start)\n  - [Available options for MongoMemoryServer](#available-options-for-mongomemoryserver)\n  - [Replica Set start](#replica-set-start)\n  - [Available options for MongoMemoryReplSet](#available-options-for-mongomemoryreplset)\n  - [Config Options](#config-options)\n  - [Simple test with MongoClient in Jest](#simple-test-with-mongoclient-in-jest)\n  - [Provide connection string to mongoose](#provide-connection-string-to-mongoose)\n  - [Test Runner Examples](#test-runner-examples)\n  - [Docker Alpine](#docker-alpine)\n  - [Enable Debug Mode](#enable-debug-mode)\n- [Contributing](#contributing)\n- [Join Our Discord Server](#join-our-discord-server)\n- [Documentation](#documentation)\n- [Credits](#credits)\n  - [Inspired by](#inspired-by)\n  - [Maintainers](#maintainers)\n- [Funding](#funding)\n  - [Contributors](#contributors)\n  - [Backers](#backers)\n  - [Sponsors](#sponsors)\n- [License](#license)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Installation\n\nThis tool provides three packages for different purposes:\n\n- With auto-download mongod binary on npm install (`mongodb-memory-server`, `mongodb-memory-server-global-*`)\n- Without auto-download on npm install (`mongodb-memory-server-core`)\n\nChoose any package, because they are the same. They differ only in the default configuration, which you may override (see section [Available options](#available-options-for-mongomemoryserver)).\n\n### Requirements\n\n- NodeJS: 16.20.1+\n- Typescript: 5.3+ (if used)\n\nAnd one of those (on Linux):\n\n- having `lsb-core` installed (or any that provides the `lsb_release` command)\n- having an `/etc/os-release` file that is compliant to the [OS-Release Spec](https://www.freedesktop.org/software/systemd/man/os-release.html)\n- having an `/etc/*-release` file that is compliant to the [OS-Release Spec](https://www.freedesktop.org/software/systemd/man/os-release.html) (and does not include `lsb`)\n- manually specify which version \u0026 system should be used\n\nOn Linux, you will also need `libcurl4` (or `libcurl3` on some older distro versions). This will probably only be an issue on \"slim\" Docker images.\n\n### Choose the Correct Package\n\n[Choose the right package for the task](https://typegoose.github.io/mongodb-memory-server/docs/guides/quick-start-guide#choose-the-right-package)\n\n### Configuring which mongod binary to use\n\nThe default behavior is that version `7.0.14` for your OS will be downloaded. By setting [Environment variables](https://typegoose.github.io/mongodb-memory-server/docs/api/config-options) you are able to specify which version and binary will be downloaded:\n\n```sh\nexport MONGOMS_DOWNLOAD_URL=https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.2.8.tgz\nexport MONGOMS_VERSION=4.2.8\n```\n\n## Usage\n\n### Simple server start\n\nA Normal Server can be easily started with:\n\n```ts\nimport { MongoMemoryServer } from 'mongodb-memory-server';\n\n// This will create an new instance of \"MongoMemoryServer\" and automatically start it\nconst mongod = await MongoMemoryServer.create();\n\nconst uri = mongod.getUri();\n\n// The Server can be stopped again with\nawait mongod.stop();\n```\n\n### Available options for MongoMemoryServer\n\nAll options with `?` are optional, some options upon specified are required.\n\n```js\nconst mongod = new MongoMemoryServer({\n  instance?: {\n    port?: number, // by default choose any free port\n    ip?: string, // by default '127.0.0.1', for binding to all IP addresses set it to `::,0.0.0.0`,\n    dbName?: string, // by default '' (empty string)\n    dbPath?: string, // by default create in temp directory\n    storageEngine?: string, // by default `ephemeralForTest`(unless mongodb 7.0.0, where its `wiredTiger`), available engines: [ 'ephemeralForTest', 'wiredTiger' ]\n    replSet?: string, // by default no replica set, replica set name\n    args?: string[], // by default no additional arguments, any additional command line arguments for `mongod` `mongod` (ex. ['--notablescan'])\n    auth?: boolean, // add \"--auth\" argument, dont use this directly use top-level \"auth\"\n  },\n  binary?: {\n    version?: string, // by default '7.0.14'\n    downloadDir?: string, // see the documentation on what is chosen by default https://typegoose.github.io/mongodb-memory-server/docs/api/config-options#download_dir\n    platform?: string, // by default os.platform()\n    arch?: string, // by default os.arch()\n    checkMD5?: boolean, // by default false OR process.env.MONGOMS_MD5_CHECK\n    systemBinary?: string, // by default undefined or process.env.MONGOMS_SYSTEM_BINARY\n  },\n  // using \"auth\" will manage \"instance.auth\"\n  auth?: {\n    // enable needs to be set to \"true\", otherwise automatic user creation is by default disabled\n    enable?: boolean, // enable automatic user creation\n    customRootName?: string, // by default \"mongodb-memory-server-root\"\n    customRootPwd?: string, // by default \"rootuser\"\n    force?: boolean, // force creation of users\n    keyfileContent?: string, // by default \"0123456789\" (only useful for replsets)\n    extraUsers?: [{\n      // see mongodb documentation https://docs.mongodb.com/manual/reference/method/db.createUser/#definition)\n      createUser: string, // user name\n      pwd: string, // user password\n      roles: UserRoles[], // user roles\n      database?: string, // which database the user is created on\n      customData?: Record\u003cstring, any\u003e, // any arbitrary information, see mongodb documentation\n      mechanisms?: ('SCRAM-SHA-1' | 'SCRAM-SHA-256')[],\n      authenticationRestrictions?: {\n        clientSource?: string;\n        serverAddress?: string;\n      }[],\n      digestPassword?: boolean,\n    }],\n  },\n});\n```\n\n### Replica Set start\n\nA ReplicaSet can be easily started with:\n\n```ts\nimport { MongoMemoryReplSet } from 'mongodb-memory-server';\n\n// This will create an new instance of \"MongoMemoryReplSet\" and automatically start all Servers\nconst replset = await MongoMemoryReplSet.create({ replSet: { count: 4 } }); // This will create an ReplSet with 4 members\n\nconst uri = replset.getUri();\n\n// The ReplSet can be stopped again with\nawait replset.stop();\n```\n\n### Available options for MongoMemoryReplSet\n\nAll options are optional.\n\n```js\nconst replSet = new MongoMemoryReplSet({\n  binary: binaryOpts, // same as for MongoMemoryServer\n  instanceOpts: [\n    {\n      args, // any additional instance specific args\n      port, // port number for the instance\n      dbPath, // path to database files for this instance\n      storageEngine, // same storage engine options\n    },\n    // each entry will result in a MongoMemoryServer (replSet.count will not limit the amount spawned by \"instanceOpts\")\n  ],\n  // unless otherwise noted below these values will be in common with all instances spawned:\n  replSet: {\n    name, // replica set name (default: 'testset')\n    auth?: boolean | AutomaticAuth, // enable auth, for options see #available-options-for-mongomemoryserver\n    args, // any args specified here will be combined with any per instance args from `instanceOpts`\n    count, // number of additional `mongod` processes to start (will not start any extra if instanceOpts.length \u003e replSet.count); (default: 1)\n    dbName, // default database for db URI strings\n    ip, // by default '127.0.0.1', for binding to all IP addresses set it to `::,0.0.0.0`\n    spawn, // spawn options when creating the child processes\n    storageEngine, // default storage engine for instance. (Can be overridden per instance)\n    configSettings: {\n      // Optional settings for 'replSetInitiate' command. See https://docs.mongodb.com/manual/reference/command/replSetInitiate/\n      chainingAllowed: boolean, // When true it allows secondary members to replicate from other secondary members. When false, secondaries can replicate only from the primary.\n      heartbeatTimeoutSecs: number, // Number of seconds that the replica set members wait for a successful heartbeat from each other. If a member does not respond in time, other members mark the delinquent member as inaccessible.\n      heartbeatIntervalMillis: number, // The frequency in milliseconds of the heartbeats.\n      electionTimeoutMillis: number, // The time limit in milliseconds for detecting when a replica set’s primary is unreachable.\n      catchUpTimeoutMillis: number, // Time limit for a newly elected primary to sync (catch up) with the other replica set members that may have more recent writes.\n    },\n  },\n});\n```\n\n### Config Options\n\n[Documentation of Config Options](https://typegoose.github.io/mongodb-memory-server/docs/api/config-options)\n\n### Simple test with MongoClient in Jest\n\nA example test file for a *single* [MongoMemoryServer](https://github.com/typegoose/mongodb-memory-server/blob/master/packages/mongodb-memory-server-core/src/__tests__/singleDB.test.ts) within jest.\n\nA example test file for *multiple* [MongoMemoryServer](https://github.com/typegoose/mongodb-memory-server/blob/master/packages/mongodb-memory-server-core/src/__tests__/multipleDB.test.ts) within jest.\n\nA example test file for a *single* [MongoMemoryReplSet](https://github.com/typegoose/mongodb-memory-server/blob/master/packages/mongodb-memory-server-core/src/__tests__/replset-multi.test.ts) within jest.\n\n### Provide connection string to mongoose\n\n```js\n// assuming mongoose@6.x\nimport mongoose from 'mongoose';\nimport { MongoMemoryServer } from 'mongodb-memory-server';\n\nconst mongoServer = await MongoMemoryServer.create();\n\n(async () =\u003e {\n  await mongoose.connect(mongoServer.getUri(), { dbName: \"verifyMASTER\" });\n\n  // your code here\n  \n  await mongoose.disconnect();\n})();\n```\n\n### Test Runner Examples\n\n[Documentation for Test Runner Integration Examples](https://typegoose.github.io/mongodb-memory-server/docs/guides/integration-examples/test-runners)\n\n### Docker Alpine\n\nThere isn't currently an official MongoDB release for alpine linux. This means that we can't pull binaries for Alpine\n(or any other platform that isn't officially supported by MongoDB), but you can use a Docker image that already has mongod\nbuilt in and then set the [`MONGOMS_SYSTEM_BINARY`](https://typegoose.github.io/mongodb-memory-server/docs/api/config-options#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.\n\n### Enable Debug Mode\n\nThe Debug mode can be enabled with an Environment-Variable or in the package.json \"config\" section:\n\n```sh\nMONGOMS_DEBUG=1 # also available case-insensitive values: \"on\" \"yes\" \"true\"\n```\n\nor\n\n```json\n{\n  \"config\": {\n    \"mongodbMemoryServer\": {\n      \"debug\": \"1\" // also available case-insensitive values: \"on\" \"yes\" \"true\"\n    }\n  }\n}\n```\n\nAlso see the [Enable Debug Mode](https://typegoose.github.io/mongodb-memory-server/docs/guides/enable-debug-mode) Guide.\n\n## Contributing\n\nContributing Guidelines are setup in [CONTRIBUTING](./.github/CONTRIBUTING.md)\n\n## Join Our Discord Server\n\nTo ask questions or just talk with us, [join our Discord Server](https://discord.gg/bgCrRP9).\n\n## Documentation\n\n- [Documentation](https://typegoose.github.io/mongodb-memory-server/docs/api/index-api)\n- [Quick start guide](https://typegoose.github.io/mongodb-memory-server/docs/guides/quick-start-guide/)\n- [Known Issues](https://typegoose.github.io/mongodb-memory-server/docs/guides/known-issues)\n\n## Credits\n\n### Inspired by\n\nInspired by alternative runners for [mongodb-prebuilt](https://github.com/winfinit/mongodb-prebuilt):\n\n- [mockgoose](https://github.com/mockgoose/Mockgoose)\n- [mongomem](https://github.com/CImrie/mongomem)\n\n### Maintainers\n\n- [@nodkz](https://github.com/nodkz) Pavel Chertorogov\n- [@AJRdev](https://github.com/AJRdev) Andre Ranarivelo\n- [@hasezoey](https://github.com/hasezoey)\n\n## Funding\n\n### Contributors\n\nThis project exists thanks to all the people who contribute.\n\u003ca href=\"graphs/contributors\"\u003e\u003cimg src=\"https://opencollective.com/mongodb-memory-server/contributors.svg?width=890\u0026button=false\" /\u003e\u003c/a\u003e\n\n### Backers\n\nThank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/mongodb-memory-server#backer)]\n\n\u003ca href=\"https://opencollective.com/mongodb-memory-server#backers\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/mongodb-memory-server/backers.svg?width=890\"\u003e\u003c/a\u003e\n\n### Sponsors\n\nSupport this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/mongodb-memory-server#sponsor)]\n\n\u003ca href=\"https://opencollective.com/mongodb-memory-server/sponsor/0/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/mongodb-memory-server/sponsor/0/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/mongodb-memory-server/sponsor/1/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/mongodb-memory-server/sponsor/1/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/mongodb-memory-server/sponsor/2/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/mongodb-memory-server/sponsor/2/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/mongodb-memory-server/sponsor/3/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/mongodb-memory-server/sponsor/3/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/mongodb-memory-server/sponsor/4/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/mongodb-memory-server/sponsor/4/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/mongodb-memory-server/sponsor/5/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/mongodb-memory-server/sponsor/5/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/mongodb-memory-server/sponsor/6/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/mongodb-memory-server/sponsor/6/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/mongodb-memory-server/sponsor/7/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/mongodb-memory-server/sponsor/7/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/mongodb-memory-server/sponsor/8/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/mongodb-memory-server/sponsor/8/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/mongodb-memory-server/sponsor/9/website\" target=\"_blank\"\u003e\u003cimg src=\"https://opencollective.com/mongodb-memory-server/sponsor/9/avatar.svg\"\u003e\u003c/a\u003e\n\n## License\n\nMIT\n","funding_links":["https://opencollective.com/mongodb-memory-server"],"categories":["TypeScript","testing","Uncategorized"],"sub_categories":["Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypegoose%2Fmongodb-memory-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftypegoose%2Fmongodb-memory-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypegoose%2Fmongodb-memory-server/lists"}