5
5
> Stability: 2 - Stable
6
6
7
7
The ` assert ` module provides a set of assertion functions for verifying
8
- invariants. The module provides a recommended [ strict assertion mode] [ ]
9
- and a more lenient legacy assertion mode.
10
-
11
- ## Class: assert.AssertionError
12
-
13
- * Extends: {errors.Error}
14
-
15
- Indicates the failure of an assertion. All errors thrown by the ` assert ` module
16
- will be instances of the ` AssertionError ` class.
17
-
18
- ### ` new assert.AssertionError(options) `
19
- <!-- YAML
20
- added: v0.1.21
21
- -->
22
-
23
- * ` options ` {Object}
24
- * ` message ` {string} If provided, the error message is set to this value.
25
- * ` actual ` {any} The ` actual ` property on the error instance.
26
- * ` expected ` {any} The ` expected ` property on the error instance.
27
- * ` operator ` {string} The ` operator ` property on the error instance.
28
- * ` stackStartFn ` {Function} If provided, the generated stack trace omits
29
- frames before this function.
30
-
31
- A subclass of ` Error ` that indicates the failure of an assertion.
32
-
33
- All instances contain the built-in ` Error ` properties (` message ` and ` name ` )
34
- and:
35
-
36
- * ` actual ` {any} Set to the ` actual ` argument for methods such as
37
- [ ` assert.strictEqual() ` ] [ ] .
38
- * ` expected ` {any} Set to the ` expected ` value for methods such as
39
- [ ` assert.strictEqual() ` ] [ ] .
40
- * ` generatedMessage ` {boolean} Indicates if the message was auto-generated
41
- (` true ` ) or not.
42
- * ` code ` {string} Value is always ` ERR_ASSERTION ` to show that the error is an
43
- assertion error.
44
- * ` operator ` {string} Set to the passed in operator value.
45
-
46
- ``` js
47
- const assert = require (' assert' );
48
-
49
- // Generate an AssertionError to compare the error message later:
50
- const { message } = new assert.AssertionError ({
51
- actual: 1 ,
52
- expected: 2 ,
53
- operator: ' strictEqual'
54
- });
55
-
56
- // Verify error output:
57
- try {
58
- assert .strictEqual (1 , 2 );
59
- } catch (err) {
60
- assert (err instanceof assert .AssertionError );
61
- assert .strictEqual (err .message , message);
62
- assert .strictEqual (err .name , ' AssertionError' );
63
- assert .strictEqual (err .actual , 1 );
64
- assert .strictEqual (err .expected , 2 );
65
- assert .strictEqual (err .code , ' ERR_ASSERTION' );
66
- assert .strictEqual (err .operator , ' strictEqual' );
67
- assert .strictEqual (err .generatedMessage , true );
68
- }
69
- ```
8
+ invariants.
70
9
71
10
## Strict assertion mode
72
11
<!-- YAML
@@ -84,9 +23,9 @@ changes:
84
23
description: Added strict assertion mode to the assert module.
85
24
-->
86
25
87
- In strict assertion mode, ` assert ` functions use the comparison in the
88
- corresponding strict functions . For example, [ ` assert.deepEqual() ` ] [ ] will
89
- behave like [ ` assert.deepStrictEqual() ` ] [ ] .
26
+ In strict assertion mode, non-strict methods behave like their corresponding
27
+ strict methods . For example, [ ` assert.deepEqual() ` ] [ ] will behave like
28
+ [ ` assert.deepStrictEqual() ` ] [ ] .
90
29
91
30
In strict assertion mode, error messages for objects display a diff. In legacy
92
31
assertion mode, error messages for objects display the objects, often truncated.
150
89
assert .deepEqual (/ a/ gi , new Date ());
151
90
```
152
91
92
+ ## Class: assert.AssertionError
93
+
94
+ * Extends: {errors.Error}
95
+
96
+ Indicates the failure of an assertion. All errors thrown by the ` assert ` module
97
+ will be instances of the ` AssertionError ` class.
98
+
99
+ ### ` new assert.AssertionError(options) `
100
+ <!-- YAML
101
+ added: v0.1.21
102
+ -->
103
+
104
+ * ` options ` {Object}
105
+ * ` message ` {string} If provided, the error message is set to this value.
106
+ * ` actual ` {any} The ` actual ` property on the error instance.
107
+ * ` expected ` {any} The ` expected ` property on the error instance.
108
+ * ` operator ` {string} The ` operator ` property on the error instance.
109
+ * ` stackStartFn ` {Function} If provided, the generated stack trace omits
110
+ frames before this function.
111
+
112
+ A subclass of ` Error ` that indicates the failure of an assertion.
113
+
114
+ All instances contain the built-in ` Error ` properties (` message ` and ` name ` )
115
+ and:
116
+
117
+ * ` actual ` {any} Set to the ` actual ` argument for methods such as
118
+ [ ` assert.strictEqual() ` ] [ ] .
119
+ * ` expected ` {any} Set to the ` expected ` value for methods such as
120
+ [ ` assert.strictEqual() ` ] [ ] .
121
+ * ` generatedMessage ` {boolean} Indicates if the message was auto-generated
122
+ (` true ` ) or not.
123
+ * ` code ` {string} Value is always ` ERR_ASSERTION ` to show that the error is an
124
+ assertion error.
125
+ * ` operator ` {string} Set to the passed in operator value.
126
+
127
+ ``` js
128
+ const assert = require (' assert' );
129
+
130
+ // Generate an AssertionError to compare the error message later:
131
+ const { message } = new assert.AssertionError ({
132
+ actual: 1 ,
133
+ expected: 2 ,
134
+ operator: ' strictEqual'
135
+ });
136
+
137
+ // Verify error output:
138
+ try {
139
+ assert .strictEqual (1 , 2 );
140
+ } catch (err) {
141
+ assert (err instanceof assert .AssertionError );
142
+ assert .strictEqual (err .message , message);
143
+ assert .strictEqual (err .name , ' AssertionError' );
144
+ assert .strictEqual (err .actual , 1 );
145
+ assert .strictEqual (err .expected , 2 );
146
+ assert .strictEqual (err .code , ' ERR_ASSERTION' );
147
+ assert .strictEqual (err .operator , ' strictEqual' );
148
+ assert .strictEqual (err .generatedMessage , true );
149
+ }
150
+ ```
151
+
153
152
## ` assert(value[, message]) `
154
153
<!-- YAML
155
154
added: v0.5.9
0 commit comments