-
Notifications
You must be signed in to change notification settings - Fork 30.5k
/
Copy pathassert.markdown
130 lines (90 loc) Β· 3.51 KB
1
# Assert
2
3
Stability: 3 - Locked
4
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.
8
9
## assert.fail(actual, expected, message, operator)
10
11
12
Throws an exception that displays the values for `actual` and `expected`
separated by the provided operator.
13
14
## assert(value[, message]), assert.ok(value[, message])
15
16
Tests if value is truthy. It is equivalent to
17
`assert.equal(!!value, true, message)`.
18
19
## assert.equal(actual, expected[, message])
20
21
Tests shallow, coercive equality with the equal comparison operator ( `==` ).
22
23
## assert.notEqual(actual, expected[, message])
24
25
26
Tests shallow, coercive inequality with the not equal comparison operator
( `!=` ).
27
28
## assert.deepEqual(actual, expected[, message])
29
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'));
41
42
## assert.notDeepEqual(actual, expected[, message])
43
44
Tests for any deep inequality. Opposite of `assert.deepEqual`.
45
46
## assert.strictEqual(actual, expected[, message])
47
48
Tests strict equality as determined by the strict equality operator ( `===` ).
49
50
## assert.notStrictEqual(actual, expected[, message])
51
52
53
Tests strict inequality as determined by the strict not equal operator
( `!==` ).
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`.
63
64
## assert.throws(block[, error][, message])
65
66
Expects `block` to throw an error. `error` can be a constructor, `RegExp`, or
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) {
94
95
if ( (err instanceof Error) && /value/.test(err) ) {
return true;
96
97
98
99
}
},
"unexpected error"
);
100
101
## assert.doesNotThrow(block[, error][, message])
102
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
);
126
127
## assert.ifError(value)
128
129
130
Throws `value` if `value` is truthy. This is useful when testing the `error`
argument in callbacks.