Skip to content

Latest commit

Β 

History

History
130 lines (90 loc) Β· 3.51 KB

assert.markdown

File metadata and controls

130 lines (90 loc) Β· 3.51 KB
Β 
Feb 29, 2012
Feb 29, 2012
1
# Assert
Oct 28, 2010
Oct 28, 2010
2
Oct 19, 2015
Oct 19, 2015
3
Stability: 3 - Locked
Mar 4, 2012
Mar 4, 2012
4
Oct 19, 2015
Oct 19, 2015
5
6
7
This module is used so that Node.js can test itself. It can be accessed with
`require('assert')`. However, it is recommended that a userland assertion
library be used instead.
Oct 28, 2010
Oct 28, 2010
8
Feb 29, 2012
Feb 29, 2012
9
## assert.fail(actual, expected, message, operator)
Oct 28, 2010
Oct 28, 2010
10
Sep 23, 2015
Sep 23, 2015
11
12
Throws an exception that displays the values for `actual` and `expected`
separated by the provided operator.
Oct 28, 2010
Oct 28, 2010
13
Feb 13, 2015
Feb 13, 2015
14
## assert(value[, message]), assert.ok(value[, message])
Oct 28, 2010
Oct 28, 2010
15
Sep 23, 2015
Sep 23, 2015
16
Tests if value is truthy. It is equivalent to
Nov 3, 2015
Nov 3, 2015
17
`assert.equal(!!value, true, message)`.
Oct 28, 2010
Oct 28, 2010
18
Sep 25, 2014
Sep 25, 2014
19
## assert.equal(actual, expected[, message])
Oct 28, 2010
Oct 28, 2010
20
Nov 21, 2010
Nov 21, 2010
21
Tests shallow, coercive equality with the equal comparison operator ( `==` ).
Oct 28, 2010
Oct 28, 2010
22
Sep 25, 2014
Sep 25, 2014
23
## assert.notEqual(actual, expected[, message])
Oct 28, 2010
Oct 28, 2010
24
Sep 19, 2015
Sep 19, 2015
25
26
Tests shallow, coercive inequality with the not equal comparison operator
( `!=` ).
Oct 28, 2010
Oct 28, 2010
27
Sep 25, 2014
Sep 25, 2014
28
## assert.deepEqual(actual, expected[, message])
Oct 28, 2010
Oct 28, 2010
29
Oct 19, 2015
Oct 19, 2015
30
31
32
33
34
35
36
37
38
39
40
Tests for deep equality. Primitive values are compared with the equal
comparison operator ( `==` ).
This only considers enumerable properties. It does not test object prototypes,
attached symbols, or non-enumerable properties. This can lead to some
potentially surprising results. For example, this does not throw an
`AssertionError` because the properties on the `Error` object are
non-enumerable:
// WARNING: This does not throw an AssertionError!
assert.deepEqual(Error('a'), Error('b'));
Oct 28, 2010
Oct 28, 2010
41
Sep 25, 2014
Sep 25, 2014
42
## assert.notDeepEqual(actual, expected[, message])
Oct 28, 2010
Oct 28, 2010
43
Feb 9, 2015
Feb 9, 2015
44
Tests for any deep inequality. Opposite of `assert.deepEqual`.
Oct 28, 2010
Oct 28, 2010
45
Sep 25, 2014
Sep 25, 2014
46
## assert.strictEqual(actual, expected[, message])
Oct 28, 2010
Oct 28, 2010
47
Sep 19, 2015
Sep 19, 2015
48
Tests strict equality as determined by the strict equality operator ( `===` ).
Oct 28, 2010
Oct 28, 2010
49
Sep 25, 2014
Sep 25, 2014
50
## assert.notStrictEqual(actual, expected[, message])
Oct 28, 2010
Oct 28, 2010
51
Sep 19, 2015
Sep 19, 2015
52
53
Tests strict inequality as determined by the strict not equal operator
( `!==` ).
Feb 9, 2015
Feb 9, 2015
54
55
56
57
58
59
60
61
62
## assert.deepStrictEqual(actual, expected[, message])
Tests for deep equality. Primitive values are compared with the strict equality
operator ( `===` ).
## assert.notDeepStrictEqual(actual, expected[, message])
Tests for deep inequality. Opposite of `assert.deepStrictEqual`.
Oct 28, 2010
Oct 28, 2010
63
Sep 29, 2014
Sep 29, 2014
64
## assert.throws(block[, error][, message])
Oct 28, 2010
Oct 28, 2010
65
Sep 19, 2015
Sep 19, 2015
66
Expects `block` to throw an error. `error` can be a constructor, `RegExp`, or
Dec 2, 2010
Dec 2, 2010
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
validation function.
Validate instanceof using constructor:
assert.throws(
function() {
throw new Error("Wrong value");
},
Error
);
Validate error message using RegExp:
assert.throws(
function() {
throw new Error("Wrong value");
},
/value/
);
Custom error validation:
assert.throws(
function() {
throw new Error("Wrong value");
},
function(err) {
Dec 21, 2010
Dec 21, 2010
94
95
if ( (err instanceof Error) && /value/.test(err) ) {
return true;
Dec 2, 2010
Dec 2, 2010
96
97
98
99
}
},
"unexpected error"
);
Oct 28, 2010
Oct 28, 2010
100
Sep 23, 2015
Sep 23, 2015
101
## assert.doesNotThrow(block[, error][, message])
Oct 28, 2010
Oct 28, 2010
102
Sep 23, 2015
Sep 23, 2015
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Expects `block` not to throw an error. See [assert.throws()](#assert_assert_throws_block_error_message) for more details.
If `block` throws an error and if it is of a different type from `error`, the
thrown error will get propagated back to the caller. The following call will
throw the `TypeError`, since we're not matching the error types in the
assertion.
assert.doesNotThrow(
function() {
throw new TypeError("Wrong value");
},
SyntaxError
);
In case `error` matches with the error thrown by `block`, an `AssertionError`
is thrown instead.
assert.doesNotThrow(
function() {
throw new TypeError("Wrong value");
},
TypeError
);
Oct 28, 2010
Oct 28, 2010
126
Feb 29, 2012
Feb 29, 2012
127
## assert.ifError(value)
Oct 28, 2010
Oct 28, 2010
128
Sep 19, 2015
Sep 19, 2015
129
130
Throws `value` if `value` is truthy. This is useful when testing the `error`
argument in callbacks.