1
|
# assert
|
2
|
|
3
|
[![Build Status](https://travis-ci.org/browserify/commonjs-assert.svg?branch=master)](https://travis-ci.org/browserify/commonjs-assert)
|
4
|
|
5
|
This module is used for writing unit tests for your applications, you can access it with `require('assert')`.
|
6
|
|
7
|
It aims to be fully compatibe with the [node.js assert module](http://nodejs.org/api/assert.html), same API and same behavior, just adding support for web browsers.
|
8
|
The API and code may contain traces of the [CommonJS Unit Testing 1.0 spec](http://wiki.commonjs.org/wiki/Unit_Testing/1.0) which they were based on, but both have evolved significantly since then.
|
9
|
|
10
|
A `strict` and a `legacy` mode exist, while it is recommended to only use `strict mode`.
|
11
|
|
12
|
## Strict mode
|
13
|
|
14
|
When using the `strict mode`, any `assert` function will use the equality used in the strict function mode. So `assert.deepEqual()` will, for example, work the same as `assert.deepStrictEqual()`.
|
15
|
|
16
|
It can be accessed using:
|
17
|
|
18
|
```js
|
19
|
const assert = require('assert').strict;
|
20
|
```
|
21
|
|
22
|
## Legacy mode
|
23
|
|
24
|
> Deprecated: Use strict mode instead.
|
25
|
|
26
|
When accessing `assert` directly instead of using the `strict` property, the
|
27
|
[Abstract Equality Comparison](https://tc39.github.io/ecma262/#sec-abstract-equality-comparison) will be used for any function without a
|
28
|
"strict" in its name (e.g. `assert.deepEqual()`).
|
29
|
|
30
|
It can be accessed using:
|
31
|
|
32
|
```js
|
33
|
const assert = require('assert');
|
34
|
```
|
35
|
|
36
|
It is recommended to use the `strict mode` instead as the Abstract Equality Comparison can often have surprising results. Especially
|
37
|
in case of `assert.deepEqual()` as the used comparison rules there are very lax.
|
38
|
|
39
|
E.g.
|
40
|
|
41
|
```js
|
42
|
// WARNING: This does not throw an AssertionError!
|
43
|
assert.deepEqual(/a/gi, new Date());
|
44
|
```
|
45
|
|
46
|
|
47
|
## assert.fail(actual, expected, message, operator)
|
48
|
Throws an exception that displays the values for actual and expected separated by the provided operator.
|
49
|
|
50
|
## assert(value, message), assert.ok(value, [message])
|
51
|
Tests if value is truthy, it is equivalent to assert.equal(true, !!value, message);
|
52
|
|
53
|
## assert.equal(actual, expected, [message])
|
54
|
Tests shallow, coercive equality with the equal comparison operator ( == ).
|
55
|
|
56
|
## assert.notEqual(actual, expected, [message])
|
57
|
Tests shallow, coercive non-equality with the not equal comparison operator ( != ).
|
58
|
|
59
|
## assert.deepEqual(actual, expected, [message])
|
60
|
Tests for deep equality.
|
61
|
|
62
|
## assert.deepStrictEqual(actual, expected, [message])
|
63
|
Tests for deep equality, as determined by the strict equality operator ( === )
|
64
|
|
65
|
## assert.notDeepEqual(actual, expected, [message])
|
66
|
Tests for any deep inequality.
|
67
|
|
68
|
## assert.strictEqual(actual, expected, [message])
|
69
|
Tests strict equality, as determined by the strict equality operator ( === )
|
70
|
|
71
|
## assert.notStrictEqual(actual, expected, [message])
|
72
|
Tests strict non-equality, as determined by the strict not equal operator ( !== )
|
73
|
|
74
|
## assert.throws(block, [error], [message])
|
75
|
Expects block to throw an error. error can be constructor, regexp or validation function.
|
76
|
|
77
|
Validate instanceof using constructor:
|
78
|
|
79
|
```javascript
|
80
|
assert.throws(function() { throw new Error("Wrong value"); }, Error);
|
81
|
```
|
82
|
|
83
|
Validate error message using RegExp:
|
84
|
|
85
|
```javascript
|
86
|
assert.throws(function() { throw new Error("Wrong value"); }, /value/);
|
87
|
```
|
88
|
|
89
|
Custom error validation:
|
90
|
|
91
|
```javascript
|
92
|
assert.throws(function() {
|
93
|
throw new Error("Wrong value");
|
94
|
}, function(err) {
|
95
|
if ( (err instanceof Error) && /value/.test(err) ) {
|
96
|
return true;
|
97
|
}
|
98
|
}, "unexpected error");
|
99
|
```
|
100
|
|
101
|
## assert.doesNotThrow(block, [message])
|
102
|
Expects block not to throw an error, see assert.throws for details.
|
103
|
|
104
|
## assert.ifError(value)
|
105
|
Tests if value is not a false value, throws if it is a true value. Useful when testing the first argument, error in callbacks.
|