# @w-schmitt/jest-mongodb

> Run your tests using Jest & MongoDB in Memory server

Latest version **1.0.2** (published 2019-05-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @w-schmitt/jest-mongodb
pnpm add @w-schmitt/jest-mongodb
yarn add @w-schmitt/jest-mongodb
bun add @w-schmitt/jest-mongodb
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2019-05-23 |
| First published | 2019-05-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8 |
| Dependencies | 4 |
| Unpacked size | 10.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Vlad Holubiev |
| Maintainers | w-schmitt |
| Keywords | jest, mongodb, mongodb local, jest preset, jest environment |

## Links

- npm: https://www.npmjs.com/package/@w-schmitt/jest-mongodb
- Repository: shelfio/W-Schmitt/jest-mongodb
- npm.io page: https://npm.io/package/@w-schmitt/jest-mongodb

## Dependencies (4)

- [cwd](https://npm.io/package/cwd.md) 0.10.0
- [debug](https://npm.io/package/debug.md) 4.1.1
- [mongodb](https://npm.io/package/mongodb.md) 3.2.5
- [mongodb-memory-server-global](https://npm.io/package/mongodb-memory-server-global.md) ^5.1.2

## 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
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2019-05-23
- 1.0.1 — 2019-05-23

## README

# jest-mongodb [![CircleCI](https://circleci.com/gh/shelfio/jest-mongodb/tree/master.svg?style=svg)](https://circleci.com/gh/shelfio/jest-mongodb/tree/master) ![](https://img.shields.io/badge/code_style-prettier-ff69b4.svg) [![npm (scoped)](https://img.shields.io/npm/v/@shelf/jest-mongodb.svg)](https://www.npmjs.com/package/@shelf/jest-mongodb)

> Jest preset to run MongoDB memory server

Same as [https://github.com/shelfio/jest-mongodb](shelfio)'s module, but with `mongodb-memory-server-global` replacing `mongodb-memory-server` for easier CI usage.

## Usage

### 0. Install

```
$ yarn add @shelf/jest-mongodb --dev
```

### 1. Create `jest.config.js`

```js
module.exports = {
  preset: '@shelf/jest-mongodb'
};
```

If you have a custom `jest.config.js` make sure you remove `testEnvironment` property, otherwise it will conflict with the preset.

### 2. Create `jest-mongodb-config.js`

See [mongodb-memory-server](https://github.com/nodkz/mongodb-memory-server#available-options)

```js
module.exports = {
  mongodbMemoryServerOptions: {
    instance: {
      dbName: 'jest'
    },
    binary: {
      version: '3.6.10',
      skipMD5: true
    },
    autoStart: false
  }
};
```

### 3. Configure MongoDB client

```js
const {MongoClient} = require('mongodb');

describe('insert', () => {
  let connection;
  let db;

  beforeAll(async () => {
    connection = await MongoClient.connect(global.__MONGO_URI__, {useNewUrlParser: true});
    db = await connection.db(global.__MONGO_DB_NAME__);
  });

  afterAll(async () => {
    await connection.close();
  });
});
```

### 4. PROFIT! Write tests

```js
it('should insert a doc into collection', async () => {
  const users = db.collection('users');

  const mockUser = {_id: 'some-user-id', name: 'John'};
  await users.insertOne(mockUser);

  const insertedUser = await users.findOne({_id: 'some-user-id'});
  expect(insertedUser).toEqual(mockUser);
});
```

Cache MongoDB binary in CI by putting this folder to the list of cached paths: `./node_modules/.cache/mongodb-memory-server/mongodb-binaries`

You can enable debug logs by setting environment variable `DEBUG=jest-mongodb:*`

## See Also

- [jest-dynamodb](https://github.com/shelfio/jest-dynamodb)

## License

MIT © [Shelf](https://shelf.io)

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