# mockodb

> In-memory mock for MongoDB in unit tests.

Latest version **0.6.1** (published 2018-07-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install mockodb
pnpm add mockodb
yarn add mockodb
bun add mockodb
```

Provides the command `mockodb-preload`.

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: insecure dependencies; abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.1 |
| Published | 2018-07-23 |
| First published | 2018-07-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 168 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Y0hy0h |
| Maintainers | y0hy0h |
| Keywords | mongodb, mongo, mock, unit test, test, testing, in-memory, fast, typescript |

## Links

- npm: https://www.npmjs.com/package/mockodb
- Repository: https://github.com/y0hy0h/mockodb
- Homepage: https://github.com/y0hy0h/mockodb#readme
- Issues: https://github.com/y0hy0h/mockodb/issues
- npm.io page: https://npm.io/package/mockodb

## Dependencies (4)

- [uuid](https://npm.io/package/uuid.md) ^3.3.2
- [mongodb](https://npm.io/package/mongodb.md) ~3.0.11
- [get-port](https://npm.io/package/get-port.md) ^3.2.0
- [mongodb-prebuilt](https://npm.io/package/mongodb-prebuilt.md) github:y0hy0h/mongodb-prebuilt#1a5821446426570946f42277b145fe7a81baa696

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [@temporal-architect/claude-plugin](https://npm.io/package/@temporal-architect/claude-plugin.md) — 335 weekly downloads

## Recent versions

- 0.6.1 (latest) — 2018-07-23
- 0.6.0 — 2018-07-20
- 0.5.0 — 2018-07-18
- 0.4.2 — 2018-07-16
- 0.4.1 — 2018-07-16
- 0.4.0 — 2018-07-16
- 0.3.1 — 2018-07-16
- 0.3.0 — 2018-07-13
- 0.2.0 — 2018-07-13
- 0.1.1 — 2018-07-12
- 0.1.0 — 2018-07-12

## README

# MockoDB

[![npm](https://img.shields.io/npm/v/mockodb.svg)][npm]
[![build status](https://travis-ci.org/Y0hy0h/mockodb.svg?branch=master)](https://travis-ci.org/Y0hy0h/mockodb)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://prettier.io/)

In-memory mock for MongoDB in unit tests.

> Inspired by [mongo-unit].

## Installation

MockoDB is [available on NPM][npm].

```bash
yarn add mockodb
npm install --save mockodb
```

## Usage

```typescript
import { MockoDb } from "mockodb";
import { MongoClient } from "mongodb";

async function demo() {
  const mockoDb = await MockoDb.boot();

  // You can now connect to the database.
  const client = await MongoClient.connect(mockoDb.url.href);
  ...

  // At any time you can drop all databases.
  await mockoDb.reset();
  ...

  // You need to take care of shutting down the db.
  await mockoDb.shutdown();
}
```

You can also control individual databases.

```typescript
import { MockoDb } from "mockodb";
import { MongoClient } from "mongodb";

async function demo() {
  const mockoDb = await MockoDb.boot();
  // Open a new database with a random name.
  const dbHandle = await mockoDb.open();
  const client = await MongoClient.connect(dbHandle.url.href);
  ...

  // Reset only that database.
  await dbHandle.drop();

  await mockoDb.shutdown();
}
```

### Preloading

Note that `MockoDb.boot()` might attempt to download the MongoDB binaries on the
first run. You can preload those libraries explicitly before your tests run
using the `preload()` function:

```typescript
import { preload } from "mockodb";

describe("test suite", () => {
  beforeAll(async () => {
    jest.setTimeout(100_000); // Preload might take a while.
    await preload();
  });

  // ...
});
```

Alternatively, for a more direct use inside your CI's script, this package
exposes the `mockodb-preload` command in its bin:

```bash
yarn install
mockodb-preload
yarn test
```

### Download Directory

MockoDB will download the MongoDB binaries into its folder. Therefore you can
simply cache your `node_modules` on your CI, making it download the MongoDB
binaries once on the first run and whenever you clear your cache.

[npm]: https://www.npmjs.com/package/mockodb
[mongo-unit]: https://github.com/mikhail-angelov/mongo-unit

---
_Source: https://npm.io/package/mockodb · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
