# mongo-assert

> Mongo assertion library

Latest version **0.3.1** (published 2021-05-22) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install mongo-assert
pnpm add mongo-assert
yarn add mongo-assert
bun add mongo-assert
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.3.1 |
| Published | 2021-05-22 |
| First published | 2019-04-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 2 |
| Unpacked size | 5.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Alexander Mac |
| Maintainers | alexandermac |
| Keywords | mongo, mongodb, mongoose, assert, assertion, test |

## Links

- npm: https://www.npmjs.com/package/mongo-assert
- Repository: https://github.com/AlexanderMac/mongo-assert
- Homepage: https://github.com/AlexanderMac/mongo-assert#readme
- Issues: https://github.com/AlexanderMac/mongo-assert/issues
- npm.io page: https://npm.io/package/mongo-assert

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [n-assert](https://npm.io/package/n-assert.md) ^7.0.0

## 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.3.1 (latest) — 2021-05-22
- 0.3.0 — 2021-02-26
- 0.2.0 — 2020-01-24
- 0.1.2 — 2019-04-05
- 0.1.1 — 2019-04-05
- 0.1.0 — 2019-04-05

## README

# mongo-assert

Mongo assertion library.

[![Build Status](https://github.com/AlexanderMac/mongo-assert/workflows/CI/badge.svg)](https://github.com/AlexanderMac/mongo-assert/actions?query=workflow%3ACI)
[![Code Coverage](https://codecov.io/gh/AlexanderMac/mongo-assert/branch/master/graph/badge.svg)](https://codecov.io/gh/AlexanderMac/mongo-assert)
[![npm version](https://badge.fury.io/js/mongo-assert.svg)](https://badge.fury.io/js/mongo-assert)

### Why
This library is created to assert made changes in mongodb. Useful when you want to be sure that method added, updated or deleted only one document, and other were unchanged.

### Install
```bash
$ npm i -D mongo-assert
```

### Usage
```js
const nassert = require('n-assert')
const mongoassert = require('mongo-assert')

const initialUsers = [
  {
    _id: nassert.getObjectId(),
    email: 'pen@mail.com',
    firstName: 'Piter',
    lastName: 'Pen'
  },
  {
    _id: nassert.getObjectId(),
    email: 'smith@mail.com',
    firstName: 'John',
    lastName: 'Smith'
  }
]

it('should update user', async () => {
  let filter = { _id: initialUsers[0]._id }
  let userData = {
      email: 'smith-another-email@mail.com'
  }
  let updatedUser = {
    _id: initialUsers[0]._id,
    email: 'smith-another-email@mail.com'
  }

  await User.create(initialUsers)
  await usersSrvc.updateUser({ filter, userData })

  await mongoassert.assertCollection({
    model: User,
    initialDocs: initialUsers,
    changedDoc: updatedUser,
    typeOfChange: updatedUser ? 'updated' : null,
    sortField: '_id'
  })
})
```

### API
- **assertCollection({ model, initialDocs, changedDoc, typeOfChange, sortField })**<br>
Asserts mongodb collection. Loads all documents in the collection, merges initial collection with changed document and asserts.

  - `model` - mongoose model.
  - `initialDocs` - initial documents collection.
  - `changedDoc` - changed document, must be omitted or undefined if collection is unchanged.
  - `typeOfChange` - the type of the change (_created_, _updated_, _deleted_), must be omitted if collection is unchanged.
  - `sortField` - the field which should be used for sorting actual and expected collections before asseting.

### Licence
Licensed under the MIT license.

### Author
Alexander Mac

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