-
Notifications
You must be signed in to change notification settings - Fork 30.5k
/
Copy pathassert.markdown
84 lines (54 loc) Β· 2.08 KB
1
# Assert
2
3
4
Stability: 5 - Locked
5
6
7
This module is used for writing unit tests for your applications, you can
access it with `require('assert')`.
8
## assert.fail(actual, expected, message, operator)
9
10
Throws an exception that displays the values for `actual` and `expected` separated by the provided operator.
11
12
## assert(value, message), assert.ok(value[, message])
13
14
Tests if value is truthy, it is equivalent to `assert.equal(true, !!value, message);`
15
16
## assert.equal(actual, expected[, message])
17
18
Tests shallow, coercive equality with the equal comparison operator ( `==` ).
19
20
## assert.notEqual(actual, expected[, message])
21
22
23
Tests shallow, coercive non-equality with the not equal comparison operator ( `!=` ).
24
## assert.deepEqual(actual, expected[, message])
25
26
27
Tests for deep equality.
28
## assert.notDeepEqual(actual, expected[, message])
29
30
Tests for any deep inequality.
31
32
## assert.strictEqual(actual, expected[, message])
33
34
Tests strict equality, as determined by the strict equality operator ( `===` )
35
36
## assert.notStrictEqual(actual, expected[, message])
37
38
Tests strict non-equality, as determined by the strict not equal operator ( `!==` )
39
40
## assert.throws(block[, error]\[, message])
41
42
Expects `block` to throw an error. `error` can be constructor, `RegExp` or
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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) {
70
71
if ( (err instanceof Error) && /value/.test(err) ) {
return true;
72
73
74
75
}
},
"unexpected error"
);
76
77
## assert.doesNotThrow(block[, message])
78
79
Expects `block` not to throw an error, see `assert.throws` for details.
80
81
## assert.ifError(value)
82
83
84
Tests if value is not a false value, throws if it is a true value. Useful when
testing the first argument, `error` in callbacks.