# n-assert

> Node assertion library

Latest version **7.0.0** (published 2021-02-26) · MIT license · 0 weekly downloads

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

## Install

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

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 7.0.0 |
| Published | 2021-02-26 |
| First published | 2017-06-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=12 |
| Dependencies | 3 |
| Unpacked size | 13.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Alexander Mac |
| Maintainers | alexandermac |
| Keywords | node, assert, assertion, test |

## Links

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

## Dependencies (3)

- [bson](https://npm.io/package/bson.md) ^4.0.3
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [should](https://npm.io/package/should.md) ^13.2.3

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 7.0.0 (latest) — 2021-02-26
- 5.0.2 — 2019-04-05
- 5.0.1 — 2019-04-05
- 5.0.0 — 2019-04-05
- 4.0.0 — 2018-11-15
- 3.1.0 — 2018-08-04
- 3.0.0 — 2018-01-27
- 2.2.0 — 2018-01-06
- 2.1.0 — 2017-10-02
- 2.0.0 — 2017-09-01
- 2.0.0-alpha6 — 2017-08-24
- 2.0.0-alpha5 — 2017-08-21
- 2.0.0-alpha4 — 2017-08-21
- 2.0.0-alpha3 — 2017-08-21
- 2.0.0-alpha2 — 2017-08-20
- … 5 more at https://npm.io/package/n-assert/versions

## README

# n-assert
Node.js assertion library.

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

## Why
This library is created to assert the complex objects.

```js
// Some test method returns this object
let actual = {
  _id: '5945bf36ccb3fa0011e8533c',
  name: 'John',
  email: 'john@mail.com',
  phones: [
    { type: 'mobile', number: '1234567' }
    { type: 'work', number: '567382' }
  ],
  account: {
    number: '11111',
    registered: '2010-04-21'
  },
  createdAt: '2017-02-03'
}

// Ned to do the following assertions:
//   _id is exists
//   name and email match
//   mobile phone is exists and it's a number
//   account registered match
let expected = {
  _id: '_mock_',
  name: 'John',
  email: 'john@mail.com',
  phones: [
    { type: 'mobile', number: /^d{1,10}$/ }
  ],
  account: {
    registered: '2010-04-21'
  }
}

nassert.assert(actual, expected)
```

## Commands
```bash
# Add to project, should install it as dev dependency
$ npm i -D n-assert
```

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

should('should find user by name', async () => {
  let actual = await usersSrvc.getUserByName('John')
  let expected = { /* some data */ }

  nassert.assert(actual, expected)
})
```

## API
- **assert(actual, expected, isEqual)**<br>
Asserts that `actual` is equal to `expected`.

  - `actual` - actual object.
  - `expected` - expected object.
  - `isEqual` - performs a deep assertion between `actual` and `expected` to determine that they are indentical. 

- **assertResponse(res, expectedStatus, expectedBody)**<br>
Asserts that status and body in `res` is equal to `expectedStatus` and `expectedBody`.

  - `res` - http response.
  - `expectedStatus` - expected http response status.
  - `expectedBody` - expected http response body.

- **assertFn**<br>
Asserts that stubbed function has been called or not, and if called, then with provided arguments. **Warning!** To use `sinonMatch` function, sinon instance should be initialized via `nassert.initSinon(sinon)` method.

  - `inst` - object instance.
  - `fnName` - stubbed function name in the service.
  - `callCount` - count of function calles, one by default.
  - `nCall` - validates that method was called with provided args on that call, zero by default.
  - `expectedArgs` - expected _single_ argument, should be `__without-args__` if function is called without arguments.
  - `expectedMultipleArgs` - expected _few_ arguments.

- **sinonMatch(expected)**<br>
Calls sinon.match and compares `actual` value with `expected` using `nassert.assert`. Returns `true` if values identical, otherwise throws an error. **Warning!** To use `sinonMatch` function, sinon instance should be initialized via `nassert.initSinon(sinon)` method.

- **getObjectId()**<br>
Returns new mongodb ObjectId.

- **getObjectIdStr()**<br>
Returns new mongodb ObjectId in string format.

## Author
Alexander Mac

## License
Licensed under the MIT license.

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