# assertion

> Assertion Library for Browser and Node.JS

Latest version **1.3.35** (published 2017-07-22) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.35 |
| Published | 2017-07-22 |
| First published | 2014-02-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Alexander Kit |
| Maintainers | tenbits |
| Keywords | assert, assertions, test, TDD, unit test |

## Links

- npm: https://www.npmjs.com/package/assertion
- npm.io page: https://npm.io/package/assertion

## 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
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.3.35 (latest) — 2017-07-22
- 1.3.34 — 2017-07-22
- 1.3.33 — 2017-07-10
- 1.3.31 — 2017-07-10
- 1.3.29 — 2017-07-10
- 1.3.27 — 2017-07-10
- 1.3.25 — 2015-09-23
- 1.3.24 — 2015-09-23
- 1.3.23 — 2015-09-23
- 1.3.22 — 2015-09-15
- 1.3.21 — 2015-08-05
- 1.3.20 — 2015-08-05
- 1.2.19 — 2015-07-10
- 1.2.18 — 2015-04-01
- 1.2.17 — 2015-04-01
- … 18 more at https://npm.io/package/assertion/versions

## README

Assertion Library for Browsers and NodeJS
----
[![Build Status](https://travis-ci.org/atmajs/assertion.png?branch=master)](https://travis-ci.org/atmajs/assertion)
[![NPM version](https://badge.fury.io/js/assertion.svg)](http://badge.fury.io/js/assertion)
[![Bower version](https://badge.fury.io/bo/assertion.svg)](http://badge.fury.io/bo/assertion)


Based on the API Interface of the NodeJS [Assert](http://nodejs.org/api/assert.html) module. And is part of the [uTest](https://github.com/atmajs/utest) Library.

_As a standalone module can be found in NPM repository_
```bash
npm install assertion
```

##### API

###### NodeJS API

- [Assert Documentation](http://nodejs.org/api/assert.html)

	```javascript
	assert
	
		equal
		notEqual
		deepEqual
		notDeepEqual
		strictEqual
		notStrictEqual
		throws
		doesNotThrow
		ifError
	```

###### Additional API

- `has / hasNot`

	**Subset matching**
	```javascript
	// Substring search
	assert.has(String, String | RegExp, ?message);
	
	// Simple property existence check
	assert.has(Object, String);
	
	// Sub-object match
	assert.has(Object, Object);
	
	// Check if item exists in set
	assert.has(Array, Primitive);
	
	// Subset match
	assert.has(Array, Array);
	```
	
	> When checking arrays or objects, deep matching is performed. See [tests](https://github.com/atmajs/assertion/blob/master/test/has.test)
	
	```javascript
	
	assert.has({
		foo: 'foo',
		bar: {
			qux: {
				qux: 'qux'
				quux: 'quux'
			},
			baz: [1, 2, 3]
		}
	}, {
		foo: null,
		bar: {
			baz: [1],
			qux: {
				qux: 'qux'
			}
		}
	});
	
	```

- `is/isNot`

	**Type check**
	```javascript
		// Check by Typename
		assert.is(Any, String, ?message)
		
		// Check by Contructor (instanceof)
		assert.is(Any, Function);
	```
	Typename is extracted from `Object.prototype.toString.call`, so these are:
	```javascript
		'String'
		'Number'
		'Null'
		'Undefined'
		'Function'
		'RegExp'
		'Date'
		'Object' // any `object` will pass here
		'HTML**' // DOM Node, e.g. HTMLBodyElement
		'CustomEvent'
		...
		all other built-in types
	```

- `lessThan` `lessThanOrEqaul` `greaterThan` `greaterThanOrEqual`

	Compares two digits

- Aliases

	There are also aliases (_which can be set to globals, to simplify the write-read of tests_)
	
	```javascript
	assert.eq_      === assert.equal
	assert.notEq_   === assert.notEqual
	
	assert.lt_      === assert.lessThan
	assert.lte_     === assert.lessThanOrEqaul
	assert.gt_      === assert.greaterThan
	assert.gt_      === assert.greaterThanOrEqual
	
	assert.deepEq_  === assert.deepEqual
	assert.notDeepEq_  === assert.notDeepEqual
	```


- jQuery

	**jQuery Assertion Extensions (_alias name syntax_)**
	```javascript
		$.fn.eq_
		$.fn.notEq_
		$.fn.deepEq_
		$.fn.notDeepEq_
		$.fn.has_
		$.fn.hasNot_
		$.fn.lt_
		$.fn.lte_
		$.fn.gt_
		$.fn.gte_
	```
	Functions API:
	- Get Property
		- ``` (Key, Expected) ```
		- ``` ([Key, Expected], message) ```
	- Function call
		- ``` (FnName [, ...arguments], Expected) ```
		- ``` ([FnName [, ...arguments], Expected], message) ```
	
	**`has/hasNot`** 
	- Node Find/Filter Assertions
		- ``` (Selector, ?ExpectedCount) ```
	
	
	Example:
	```javascript
	// <div class='container' id='foo'>
	//		<h4>Baz</h4>
	//		<span>Qux</span>
	// </div>
	
	$('.container')
		.eq_('length', 1)
		.eq_('attr', 'id', 'foo')
		.eq_('hasClass', 'container', true)
		
		.children()
		.eq_('length', 2)
		.has_('html', 'span')
		
		.filter('h4')
		.eq_('length', 1)
		.eq_('text', 'Baz')
		
		// addition sample
		.is_('visible'),
		.is_('hidden')
		.eq_('css', 'border-left-width', '2px')
		;
		
	$('.container')
		.has_('h4')
		.hasNot_('h1')
		;
	```
	

- Assert callbacks calls

	- `await`
	
		_**Wait for a callback**_
		
		Creates a wrapper function to ensure that the function is called.
		```javascript
			// ! Arguments order does not matter
			var fn = assert.await(
				String   /* optional - name of this wrapper*/
				Function /* optional - wrap the function*/,
				Object   /* optional - use binded context*/,
				Number   /* optional - expectation count, default is `1`*/
			);
			
			// creates item in assert.callbacks
			[
				{
					name: String,
					error: Error, // to receive the stack trace
					count: Number
				}
			];
			
			// after the `fn` function is called `count` times, then the object is removed
			// from the callbacks set
			
			
			// Example
			var fn = assert.await();
			assert.callbacks.length === 1;
			try {
				throw new Error()
			} catch {
				fn();
			}
			
			assert.callbacks.length === 0;
			
		```
	- `avoid`
	
		_Unexpect more then N function calls_
		```javascript
		// ! Arguments order does not matter
		var fn = assert.avoid(
			String   /* optional - name of this wrapper*/
			Function /* optional - wrap the function*/,
			Object   /* optional - use binded context*/,
			Number   /* optional - amount of allowed calls, default is `0`*/
		);
		
		fooDfr()
			.fail(assert.avoid())
			.done(function(){
				// ..
			})
			
		
		```
	
- Listener
	
	You can attach listener to the assertions.
	Event Types:
	- `start`
	- `fail`
		> if `fail` type listener is attached, then exceptions are not thrown.
	- `success`
	
	```javascript
	// sample
	assert.on('fail', function(error){
		error instanceof assert.AssertionError;
	});
	```

:copyright: MIT - The Atma.js Project

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