-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassert.html
More file actions
1257 lines (1186 loc) · 90.7 KB
/
assert.html
File metadata and controls
1257 lines (1186 loc) · 90.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Assert | Node.js v12.0.0 Documentation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/hljs.css">
<link rel="canonical" href="https://nodejs.org/api/assert.html">
</head>
<body class="alt apidoc" id="api-section-assert">
<div id="content" class="clearfix">
<div id="column2" class="interior">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
Node.js
</a>
</div>
<ul>
<li><a href="documentation.html" class="nav-documentation">About these Docs</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage & Example</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a href="assert.html" class="nav-assert active">Assertion Testing</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async Hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ Addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ Addons - N-API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child Processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command Line Options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="esm.html" class="nav-esm">ECMAScript Modules</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File System</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance Hooks</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query Strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String Decoder</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace Events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/Datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker Threads</a></li>
<li><a href="zlib.html" class="nav-zlib">ZLIB</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">GitHub Repo & Issue Tracker</a></li>
</ul>
</div>
<div id="column1" data-id="assert" class="interior">
<header>
<h1>Node.js v12.0.0 Documentation</h1>
<div id="gtoc">
<ul>
<li>
<a href="index.html" name="toc">Index</a>
</li>
<li>
<a href="all.html">View on single page</a>
</li>
<li>
<a href="assert.json">View as JSON</a>
</li>
<li class="version-picker">
<a href="#">View another version <span>▼</span></a>
<ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v11.x/api/assert.html">11.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v10.x/api/assert.html">10.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v9.x/api/assert.html">9.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v8.x/api/assert.html">8.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v7.x/api/assert.html">7.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v6.x/api/assert.html">6.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v5.x/api/assert.html">5.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v4.x/api/assert.html">4.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/assert.html">0.12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/assert.html">0.10.x</a></li></ol>
</li>
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/assert.md"><span class="github_icon"><svg height="16" width="16" viewBox="0 0 16.1 16.1" fill="currentColor"><path d="M8 0a8 8 0 0 0-2.5 15.6c.4 0 .5-.2.5-.4v-1.5c-2 .4-2.5-.5-2.7-1 0-.1-.5-.9-.8-1-.3-.2-.7-.6 0-.6.6 0 1 .6 1.2.8.7 1.2 1.9 1 2.4.7 0-.5.2-.9.5-1-1.8-.3-3.7-1-3.7-4 0-.9.3-1.6.8-2.2 0-.2-.3-1 .1-2 0 0 .7-.3 2.2.7a7.4 7.4 0 0 1 4 0c1.5-1 2.2-.8 2.2-.8.5 1.1.2 2 .1 2.1.5.6.8 1.3.8 2.2 0 3-1.9 3.7-3.6 4 .3.2.5.7.5 1.4v2.2c0 .2.1.5.5.4A8 8 0 0 0 16 8a8 8 0 0 0-8-8z"/></svg></span>Edit on GitHub</a></li>
</ul>
</div>
<hr>
</header>
<div id="toc">
<h2>Table of Contents</h2>
<ul>
<li>
<p><span class="stability_2"><a href="#assert_assert">Assert</a></span></p>
<ul>
<li>
<p><a href="#assert_class_assert_assertionerror">Class: assert.AssertionError</a></p>
<ul>
<li><a href="#assert_new_assert_assertionerror_options">new assert.AssertionError(options)</a></li>
</ul>
</li>
<li><a href="#assert_strict_mode">Strict mode</a></li>
<li><a href="#assert_legacy_mode">Legacy mode</a></li>
<li><a href="#assert_assert_value_message">assert(value[, message])</a></li>
<li><a href="#assert_assert_deepequal_actual_expected_message">assert.deepEqual(actual, expected[, message])</a></li>
<li>
<p><a href="#assert_assert_deepstrictequal_actual_expected_message">assert.deepStrictEqual(actual, expected[, message])</a></p>
<ul>
<li><a href="#assert_comparison_details">Comparison details</a></li>
</ul>
</li>
<li><a href="#assert_assert_doesnotreject_asyncfn_error_message">assert.doesNotReject(asyncFn[, error][, message])</a></li>
<li><a href="#assert_assert_doesnotthrow_fn_error_message">assert.doesNotThrow(fn[, error][, message])</a></li>
<li><a href="#assert_assert_equal_actual_expected_message">assert.equal(actual, expected[, message])</a></li>
<li><a href="#assert_assert_fail_message">assert.fail([message])</a></li>
<li><span class="stability_0"><a href="#assert_assert_fail_actual_expected_message_operator_stackstartfn">assert.fail(actual, expected[, message[, operator[, stackStartFn]]])</a></span></li>
<li><a href="#assert_assert_iferror_value">assert.ifError(value)</a></li>
<li><a href="#assert_assert_notdeepequal_actual_expected_message">assert.notDeepEqual(actual, expected[, message])</a></li>
<li><a href="#assert_assert_notdeepstrictequal_actual_expected_message">assert.notDeepStrictEqual(actual, expected[, message])</a></li>
<li><a href="#assert_assert_notequal_actual_expected_message">assert.notEqual(actual, expected[, message])</a></li>
<li><a href="#assert_assert_notstrictequal_actual_expected_message">assert.notStrictEqual(actual, expected[, message])</a></li>
<li><a href="#assert_assert_ok_value_message">assert.ok(value[, message])</a></li>
<li><a href="#assert_assert_rejects_asyncfn_error_message">assert.rejects(asyncFn[, error][, message])</a></li>
<li><a href="#assert_assert_strictequal_actual_expected_message">assert.strictEqual(actual, expected[, message])</a></li>
<li><a href="#assert_assert_throws_fn_error_message">assert.throws(fn[, error][, message])</a></li>
</ul>
</li>
</ul>
</div>
<div id="apicontent">
<h1>Assert<span><a class="mark" href="#assert_assert" id="assert_assert">#</a></span></h1>
<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
<p>The <code>assert</code> module provides a simple set of assertion tests that can be used to
test invariants.</p>
<p>A <code>strict</code> and a <code>legacy</code> mode exist, while it is recommended to only use
<a href="#assert_strict_mode"><code>strict mode</code></a>.</p>
<p>For more information about the used equality comparisons see
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness">MDN's guide on equality comparisons and sameness</a>.</p>
<h2>Class: assert.AssertionError<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L134">[src]</a><span><a class="mark" href="#assert_class_assert_assertionerror" id="assert_class_assert_assertionerror">#</a></span></h2>
<p>A subclass of <code>Error</code> that indicates the failure of an assertion. All errors
thrown by the <code>assert</code> module will be instances of the <code>AssertionError</code> class.</p>
<h3>new assert.AssertionError(options)<span><a class="mark" href="#assert_new_assert_assertionerror_options" id="assert_new_assert_assertionerror_options">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.21</span>
</div>
<ul>
<li>
<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p>
<ul>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> If provided, the error message is going to be set to this
value.</li>
<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The <code>actual</code> property on the error instance is going to
contain this value. Internally used for the <code>actual</code> error input in case
e.g., <a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a> is used.</li>
<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The <code>expected</code> property on the error instance is going to
contain this value. Internally used for the <code>expected</code> error input in case
e.g., <a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a> is used.</li>
<li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The <code>operator</code> property on the error instance is going
to contain this value. Internally used to indicate what operation was used
for comparison (or what assertion function triggered the error).</li>
<li><code>stackStartFn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> If provided, the generated stack trace is going to
remove all frames up to the provided function.</li>
</ul>
</li>
</ul>
<p>A subclass of <code>Error</code> that indicates the failure of an assertion.</p>
<p>All instances contain the built-in <code>Error</code> properties (<code>message</code> and <code>name</code>)
and:</p>
<ul>
<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Set to the actual value in case e.g.,
<a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a> is used.</li>
<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Set to the expected value in case e.g.,
<a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a> is used.</li>
<li><code>generatedMessage</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Indicates if the message was auto-generated
(<code>true</code>) or not.</li>
<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> This is always set to the string <code>ERR_ASSERTION</code> to indicate
that the error is actually an assertion error.</li>
<li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Set to the passed in operator value.</li>
</ul>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>);
<span class="hljs-comment">// Generate an AssertionError to compare the error message later:</span>
<span class="hljs-keyword">const</span> { message } = <span class="hljs-keyword">new</span> assert.AssertionError({
<span class="hljs-attr">actual</span>: <span class="hljs-number">1</span>,
<span class="hljs-attr">expected</span>: <span class="hljs-number">2</span>,
<span class="hljs-attr">operator</span>: <span class="hljs-string">'strictEqual'</span>
});
<span class="hljs-comment">// Verify error output:</span>
<span class="hljs-keyword">try</span> {
assert.strictEqual(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
} <span class="hljs-keyword">catch</span> (err) {
assert(err <span class="hljs-keyword">instanceof</span> assert.AssertionError);
assert.strictEqual(err.message, message);
assert.strictEqual(err.name, <span class="hljs-string">'AssertionError [ERR_ASSERTION]'</span>);
assert.strictEqual(err.actual, <span class="hljs-number">1</span>);
assert.strictEqual(err.expected, <span class="hljs-number">2</span>);
assert.strictEqual(err.code, <span class="hljs-string">'ERR_ASSERTION'</span>);
assert.strictEqual(err.operator, <span class="hljs-string">'strictEqual'</span>);
assert.strictEqual(err.generatedMessage, <span class="hljs-literal">true</span>);
}</code></pre>
<h2>Strict mode<span><a class="mark" href="#assert_strict_mode" id="assert_strict_mode">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v9.9.0</td>
<td><p>Added error diffs to the strict mode</p></td></tr>
<tr><td>v9.9.0</td>
<td><p>Added strict mode to the assert module.</p></td></tr>
<tr><td>v9.9.0</td>
<td><p><span>Added in: v9.9.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<p>When using the <code>strict mode</code>, any <code>assert</code> function will use the equality used
in the strict function mode. So <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a> will, for example,
work the same as <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a>.</p>
<p>On top of that, error messages which involve objects produce an error diff
instead of displaying both objects. That is not the case for the legacy mode.</p>
<p>It can be accessed using:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).strict;</code></pre>
<p>Example error diff:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).strict;
assert.deepEqual([[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]], <span class="hljs-number">4</span>, <span class="hljs-number">5</span>], [[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'3'</span>]], <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]);
<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
<span class="hljs-comment">// + actual - expected ... Lines skipped</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// [</span>
<span class="hljs-comment">// [</span>
<span class="hljs-comment">// ...</span>
<span class="hljs-comment">// 2,</span>
<span class="hljs-comment">// + 3</span>
<span class="hljs-comment">// - '3'</span>
<span class="hljs-comment">// ],</span>
<span class="hljs-comment">// ...</span>
<span class="hljs-comment">// 5</span>
<span class="hljs-comment">// ]</span></code></pre>
<p>To deactivate the colors, use the <code>NODE_DISABLE_COLORS</code> environment variable.
Please note that this will also deactivate the colors in the REPL.</p>
<h2>Legacy mode<span><a class="mark" href="#assert_legacy_mode" id="assert_legacy_mode">#</a></span></h2>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use strict mode instead.</div><p></p>
<p>When accessing <code>assert</code> directly instead of using the <code>strict</code> property, the
<a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a> will be used for any function without "strict"
in its name, such as <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a>.</p>
<p>It can be accessed using:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>);</code></pre>
<p>It is recommended to use the <a href="#assert_strict_mode"><code>strict mode</code></a> instead as the
<a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a> can often have surprising results. This is
especially true for <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a>, where the comparison rules are
lax:</p>
<pre><code class="language-js"><span class="hljs-comment">// WARNING: This does not throw an AssertionError!</span>
assert.deepEqual(<span class="hljs-regexp">/a/gi</span>, <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>());</code></pre>
<h2>assert(value[, message])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L73">[src]</a><span><a class="mark" href="#assert_assert_value_message" id="assert_assert_value_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.5.9</span>
</div>
<ul>
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> The input that is checked for being truthy.</li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p>An alias of <a href="#assert_assert_ok_value_message"><code>assert.ok()</code></a>.</p>
<h2>assert.deepEqual(actual, expected[, message])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L393">[src]</a><span><a class="mark" href="#assert_assert_deepequal_actual_expected_message" id="assert_assert_deepequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>Error</code> names and messages are now properly compared</p></td></tr>
<tr><td>v8.0.0</td>
<td><p>The <code>Set</code> and <code>Map</code> content is also compared</p></td></tr>
<tr><td>v6.4.0, v4.7.1</td>
<td><p>Typed array slices are handled correctly now.</p></td></tr>
<tr><td>v6.1.0, v4.5.0</td>
<td><p>Objects with circular references can be used as inputs now.</p></td></tr>
<tr><td>v5.10.1, v4.4.3</td>
<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p><strong>Strict mode</strong></p>
<p>An alias of <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a>.</p>
<p><strong>Legacy mode</strong></p>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a> instead.</div><p></p>
<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters.
Primitive values are compared with the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a>
( <code>==</code> ).</p>
<p>Only <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">enumerable "own" properties</a> are considered. The
<a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a> implementation does not test the
<a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots"><code>[[Prototype]]</code></a> of objects or enumerable own <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol"><code>Symbol</code></a>
properties. For such checks, consider using <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a>
instead. <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a> can have potentially surprising results. The
following example does not throw an <code>AssertionError</code> because the properties on
the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a> object are not enumerable:</p>
<pre><code class="language-js"><span class="hljs-comment">// WARNING: This does not throw an AssertionError!</span>
assert.deepEqual(<span class="hljs-regexp">/a/gi</span>, <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>());</code></pre>
<p>An exception is made for <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map"><code>Map</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set"><code>Set</code></a>. <code>Map</code>s and <code>Set</code>s have their
contained items compared too, as expected.</p>
<p>"Deep" equality means that the enumerable "own" properties of child objects
are evaluated also:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>);
<span class="hljs-keyword">const</span> obj1 = {
<span class="hljs-attr">a</span>: {
<span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
}
};
<span class="hljs-keyword">const</span> obj2 = {
<span class="hljs-attr">a</span>: {
<span class="hljs-attr">b</span>: <span class="hljs-number">2</span>
}
};
<span class="hljs-keyword">const</span> obj3 = {
<span class="hljs-attr">a</span>: {
<span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
}
};
<span class="hljs-keyword">const</span> obj4 = <span class="hljs-built_in">Object</span>.create(obj1);
assert.deepEqual(obj1, obj1);
<span class="hljs-comment">// OK</span>
<span class="hljs-comment">// Values of b are different:</span>
assert.deepEqual(obj1, obj2);
<span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }</span>
assert.deepEqual(obj1, obj3);
<span class="hljs-comment">// OK</span>
<span class="hljs-comment">// Prototypes are ignored:</span>
assert.deepEqual(obj1, obj4);
<span class="hljs-comment">// AssertionError: { a: { b: 1 } } deepEqual {}</span></code></pre>
<p>If the values are not equal, an <code>AssertionError</code> is thrown with a <code>message</code>
property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
parameter is undefined, a default error message is assigned. If the <code>message</code>
parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<code>AssertionError</code>.</p>
<h2>assert.deepStrictEqual(actual, expected[, message])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L421">[src]</a><span><a class="mark" href="#assert_assert_deepstrictequal_actual_expected_message" id="assert_assert_deepstrictequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v9.0.0</td>
<td><p>Enumerable symbol properties are now compared.</p></td></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>NaN</code> is now compared using the <a href="https://tc39.github.io/ecma262/#sec-samevaluezero">SameValueZero</a> comparison.</p></td></tr>
<tr><td>v8.5.0</td>
<td><p>The <code>Error</code> names and messages are now properly compared</p></td></tr>
<tr><td>v8.0.0</td>
<td><p>The <code>Set</code> and <code>Map</code> content is also compared</p></td></tr>
<tr><td>v6.4.0, v4.7.1</td>
<td><p>Typed array slices are handled correctly now.</p></td></tr>
<tr><td>v6.1.0</td>
<td><p>Objects with circular references can be used as inputs now.</p></td></tr>
<tr><td>v5.10.1, v4.4.3</td>
<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
<tr><td>v1.2.0</td>
<td><p><span>Added in: v1.2.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p>Tests for deep equality between the <code>actual</code> and <code>expected</code> parameters.
"Deep" equality means that the enumerable "own" properties of child objects
are recursively evaluated also by the following rules.</p>
<h3>Comparison details<span><a class="mark" href="#assert_comparison_details" id="assert_comparison_details">#</a></span></h3>
<ul>
<li>Primitive values are compared using the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>, used by
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is"><code>Object.is()</code></a>.</li>
<li><a href="https://tc39.github.io/ecma262/#sec-object.prototype.tostring">Type tags</a> of objects should be the same.</li>
<li><a href="https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots"><code>[[Prototype]]</code></a> of objects are compared using
the <a href="https://tc39.github.io/ecma262/#sec-strict-equality-comparison">Strict Equality Comparison</a>.</li>
<li>Only <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">enumerable "own" properties</a> are considered.</li>
<li><a href="errors.html#errors_class_error"><code>Error</code></a> names and messages are always compared, even if these are not
enumerable properties.</li>
<li>Enumerable own <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol"><code>Symbol</code></a> properties are compared as well.</li>
<li><a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript">Object wrappers</a> are compared both as objects and unwrapped values.</li>
<li><code>Object</code> properties are compared unordered.</li>
<li><code>Map</code> keys and <code>Set</code> items are compared unordered.</li>
<li>Recursion stops when both sides differ or both sides encounter a circular
reference.</li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> comparison does not rely on their values. See
below for further details.</li>
</ul>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).strict;
<span class="hljs-comment">// This fails because 1 !== '1'.</span>
assert.deepStrictEqual({ <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> }, { <span class="hljs-attr">a</span>: <span class="hljs-string">'1'</span> });
<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
<span class="hljs-comment">// + actual - expected</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// {</span>
<span class="hljs-comment">// + a: 1</span>
<span class="hljs-comment">// - a: '1'</span>
<span class="hljs-comment">// }</span>
<span class="hljs-comment">// The following objects don't have own properties</span>
<span class="hljs-keyword">const</span> date = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>();
<span class="hljs-keyword">const</span> object = {};
<span class="hljs-keyword">const</span> fakeDate = {};
<span class="hljs-built_in">Object</span>.setPrototypeOf(fakeDate, <span class="hljs-built_in">Date</span>.prototype);
<span class="hljs-comment">// Different [[Prototype]]:</span>
assert.deepStrictEqual(object, fakeDate);
<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
<span class="hljs-comment">// + actual - expected</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// + {}</span>
<span class="hljs-comment">// - Date {}</span>
<span class="hljs-comment">// Different type tags:</span>
assert.deepStrictEqual(date, fakeDate);
<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
<span class="hljs-comment">// + actual - expected</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// + 2018-04-26T00:49:08.604Z</span>
<span class="hljs-comment">// - Date {}</span>
assert.deepStrictEqual(<span class="hljs-literal">NaN</span>, <span class="hljs-literal">NaN</span>);
<span class="hljs-comment">// OK, because of the SameValue comparison</span>
<span class="hljs-comment">// Different unwrapped numbers:</span>
assert.deepStrictEqual(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Number</span>(<span class="hljs-number">1</span>), <span class="hljs-keyword">new</span> <span class="hljs-built_in">Number</span>(<span class="hljs-number">2</span>));
<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
<span class="hljs-comment">// + actual - expected</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// + [Number: 1]</span>
<span class="hljs-comment">// - [Number: 2]</span>
assert.deepStrictEqual(<span class="hljs-keyword">new</span> <span class="hljs-built_in">String</span>(<span class="hljs-string">'foo'</span>), <span class="hljs-built_in">Object</span>(<span class="hljs-string">'foo'</span>));
<span class="hljs-comment">// OK because the object and the string are identical when unwrapped.</span>
assert.deepStrictEqual(<span class="hljs-number">-0</span>, <span class="hljs-number">-0</span>);
<span class="hljs-comment">// OK</span>
<span class="hljs-comment">// Different zeros using the SameValue Comparison:</span>
assert.deepStrictEqual(<span class="hljs-number">0</span>, <span class="hljs-number">-0</span>);
<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
<span class="hljs-comment">// + actual - expected</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// + 0</span>
<span class="hljs-comment">// - -0</span>
<span class="hljs-keyword">const</span> symbol1 = <span class="hljs-built_in">Symbol</span>();
<span class="hljs-keyword">const</span> symbol2 = <span class="hljs-built_in">Symbol</span>();
assert.deepStrictEqual({ [symbol1]: <span class="hljs-number">1</span> }, { [symbol1]: <span class="hljs-number">1</span> });
<span class="hljs-comment">// OK, because it is the same symbol on both objects.</span>
assert.deepStrictEqual({ [symbol1]: <span class="hljs-number">1</span> }, { [symbol2]: <span class="hljs-number">1</span> });
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// {</span>
<span class="hljs-comment">// [Symbol()]: 1</span>
<span class="hljs-comment">// }</span>
<span class="hljs-keyword">const</span> weakMap1 = <span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakMap</span>();
<span class="hljs-keyword">const</span> weakMap2 = <span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakMap</span>([[{}, {}]]);
<span class="hljs-keyword">const</span> weakMap3 = <span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakMap</span>();
weakMap3.unequal = <span class="hljs-literal">true</span>;
assert.deepStrictEqual(weakMap1, weakMap2);
<span class="hljs-comment">// OK, because it is impossible to compare the entries</span>
<span class="hljs-comment">// Fails because weakMap3 has a property that weakMap1 does not contain:</span>
assert.deepStrictEqual(weakMap1, weakMap3);
<span class="hljs-comment">// AssertionError: Expected inputs to be strictly deep-equal:</span>
<span class="hljs-comment">// + actual - expected</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// WeakMap {</span>
<span class="hljs-comment">// + [items unknown]</span>
<span class="hljs-comment">// - [items unknown],</span>
<span class="hljs-comment">// - unequal: true</span>
<span class="hljs-comment">// }</span></code></pre>
<p>If the values are not equal, an <code>AssertionError</code> is thrown with a <code>message</code>
property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
parameter is undefined, a default error message is assigned. If the <code>message</code>
parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<code>AssertionError</code>.</p>
<h2>assert.doesNotReject(asyncFn[, error][, message])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L705">[src]</a><span><a class="mark" href="#assert_assert_doesnotreject_asyncfn_error_message" id="assert_assert_doesnotreject_asyncfn_error_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>asyncFn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type"><RegExp></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Awaits the <code>asyncFn</code> promise or, if <code>asyncFn</code> is a function, immediately
calls the function and awaits the returned promise to complete. It will then
check that the promise is not rejected.</p>
<p>If <code>asyncFn</code> is a function and it throws an error synchronously,
<code>assert.doesNotReject()</code> will return a rejected <code>Promise</code> with that error. If
the function does not return a promise, <code>assert.doesNotReject()</code> will return a
rejected <code>Promise</code> with an <a href="errors.html#errors_err_invalid_return_value"><code>ERR_INVALID_RETURN_VALUE</code></a> error. In both cases
the error handler is skipped.</p>
<p>Using <code>assert.doesNotReject()</code> is actually not useful because there is little
benefit in catching a rejection and then rejecting it again. Instead, consider
adding a comment next to the specific code path that should not reject and keep
error messages as expressive as possible.</p>
<p>If specified, <code>error</code> can be a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a> or a validation
function. See <a href="#assert_assert_throws_fn_error_message"><code>assert.throws()</code></a> for more details.</p>
<p>Besides the async nature to await the completion behaves identically to
<a href="#assert_assert_doesnotthrow_fn_error_message"><code>assert.doesNotThrow()</code></a>.</p>
<pre><code class="language-js">(<span class="hljs-keyword">async</span> () => {
<span class="hljs-keyword">await</span> assert.doesNotReject(
<span class="hljs-keyword">async</span> () => {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">TypeError</span>(<span class="hljs-string">'Wrong value'</span>);
},
<span class="hljs-built_in">SyntaxError</span>
);
})();</code></pre>
<pre><code class="language-js">assert.doesNotReject(<span class="hljs-built_in">Promise</span>.reject(<span class="hljs-keyword">new</span> <span class="hljs-built_in">TypeError</span>(<span class="hljs-string">'Wrong value'</span>)))
.then(<span class="hljs-function"><span class="hljs-params">()</span> =></span> {
<span class="hljs-comment">// ...</span>
});</code></pre>
<h2>assert.doesNotThrow(fn[, error][, message])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L701">[src]</a><span><a class="mark" href="#assert_assert_doesnotthrow_fn_error_message" id="assert_assert_doesnotthrow_fn_error_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v5.11.0, v4.4.5</td>
<td><p>The <code>message</code> parameter is respected now.</p></td></tr>
<tr><td>v4.2.0</td>
<td><p>The <code>error</code> parameter can now be an arrow function.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li><code>error</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp" class="type"><RegExp></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Asserts that the function <code>fn</code> does not throw an error.</p>
<p>Using <code>assert.doesNotThrow()</code> is actually not useful because there
is no benefit in catching an error and then rethrowing it. Instead, consider
adding a comment next to the specific code path that should not throw and keep
error messages as expressive as possible.</p>
<p>When <code>assert.doesNotThrow()</code> is called, it will immediately call the <code>fn</code>
function.</p>
<p>If an error is thrown and it is the same type as that specified by the <code>error</code>
parameter, then an <code>AssertionError</code> is thrown. If the error is of a different
type, or if the <code>error</code> parameter is undefined, the error is propagated back
to the caller.</p>
<p>If specified, <code>error</code> can be a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes"><code>Class</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions"><code>RegExp</code></a> or a validation
function. See <a href="#assert_assert_throws_fn_error_message"><code>assert.throws()</code></a> for more details.</p>
<p>The following, for instance, will throw the <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> because there is no
matching error type in the assertion:</p>
<!-- eslint-disable no-restricted-syntax -->
<pre><code class="language-js">assert.doesNotThrow(
<span class="hljs-function"><span class="hljs-params">()</span> =></span> {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">TypeError</span>(<span class="hljs-string">'Wrong value'</span>);
},
<span class="hljs-built_in">SyntaxError</span>
);</code></pre>
<p>However, the following will result in an <code>AssertionError</code> with the message
'Got unwanted exception...':</p>
<!-- eslint-disable no-restricted-syntax -->
<pre><code class="language-js">assert.doesNotThrow(
<span class="hljs-function"><span class="hljs-params">()</span> =></span> {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">TypeError</span>(<span class="hljs-string">'Wrong value'</span>);
},
<span class="hljs-built_in">TypeError</span>
);</code></pre>
<p>If an <code>AssertionError</code> is thrown and a value is provided for the <code>message</code>
parameter, the value of <code>message</code> will be appended to the <code>AssertionError</code>
message:</p>
<!-- eslint-disable no-restricted-syntax -->
<pre><code class="language-js">assert.doesNotThrow(
<span class="hljs-function"><span class="hljs-params">()</span> =></span> {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">TypeError</span>(<span class="hljs-string">'Wrong value'</span>);
},
/Wrong value/,
<span class="hljs-string">'Whoops'</span>
);
<span class="hljs-comment">// Throws: AssertionError: Got unwanted exception: Whoops</span></code></pre>
<h2>assert.equal(actual, expected[, message])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L364">[src]</a><span><a class="mark" href="#assert_assert_equal_actual_expected_message" id="assert_assert_equal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.21</span>
</div>
<ul>
<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p><strong>Strict mode</strong></p>
<p>An alias of <a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a>.</p>
<p><strong>Legacy mode</strong></p>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#assert_assert_strictequal_actual_expected_message"><code>assert.strictEqual()</code></a> instead.</div><p></p>
<p>Tests shallow, coercive equality between the <code>actual</code> and <code>expected</code> parameters
using the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a> ( <code>==</code> ).</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>);
assert.equal(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
<span class="hljs-comment">// OK, 1 == 1</span>
assert.equal(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>);
<span class="hljs-comment">// OK, 1 == '1'</span>
assert.equal(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
<span class="hljs-comment">// AssertionError: 1 == 2</span>
assert.equal({ <span class="hljs-attr">a</span>: { <span class="hljs-attr">b</span>: <span class="hljs-number">1</span> } }, { <span class="hljs-attr">a</span>: { <span class="hljs-attr">b</span>: <span class="hljs-number">1</span> } });
<span class="hljs-comment">// AssertionError: { a: { b: 1 } } == { a: { b: 1 } }</span></code></pre>
<p>If the values are not equal, an <code>AssertionError</code> is thrown with a <code>message</code>
property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
parameter is undefined, a default error message is assigned. If the <code>message</code>
parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<code>AssertionError</code>.</p>
<h2>assert.fail([message])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L89">[src]</a><span><a class="mark" href="#assert_assert_fail_message" id="assert_assert_fail_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.21</span>
</div>
<ul>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a> <strong>Default:</strong> <code>'Failed'</code></li>
</ul>
<p>Throws an <code>AssertionError</code> with the provided error message or a default error
message. If the <code>message</code> parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it
will be thrown instead of the <code>AssertionError</code>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).strict;
assert.fail();
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Failed</span>
assert.fail(<span class="hljs-string">'boom'</span>);
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: boom</span>
assert.fail(<span class="hljs-keyword">new</span> <span class="hljs-built_in">TypeError</span>(<span class="hljs-string">'need array'</span>));
<span class="hljs-comment">// TypeError: need array</span></code></pre>
<p>Using <code>assert.fail()</code> with more than two arguments is possible but deprecated.
See below for further details.</p>
<h2>assert.fail(actual, expected[, message[, operator[, stackStartFn]]])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L89">[src]</a><span><a class="mark" href="#assert_assert_fail_actual_expected_message_operator_stackstartfn" id="assert_assert_fail_actual_expected_message_operator_stackstartfn">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>Calling <code>assert.fail()</code> with more than one argument is deprecated and emits a warning.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
<li><code>operator</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <strong>Default:</strong> <code>'!='</code></li>
<li><code>stackStartFn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> <strong>Default:</strong> <code>assert.fail</code></li>
</ul>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <code>assert.fail([message])</code> or other assert
functions instead.</div><p></p>
<p>If <code>message</code> is falsy, the error message is set as the values of <code>actual</code> and
<code>expected</code> separated by the provided <code>operator</code>. If just the two <code>actual</code> and
<code>expected</code> arguments are provided, <code>operator</code> will default to <code>'!='</code>. If
<code>message</code> is provided as third argument it will be used as the error message and
the other arguments will be stored as properties on the thrown object. If
<code>stackStartFn</code> is provided, all stack frames above that function will be
removed from stacktrace (see <a href="errors.html#errors_error_capturestacktrace_targetobject_constructoropt"><code>Error.captureStackTrace</code></a>). If no arguments are
given, the default message <code>Failed</code> will be used.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).strict;
assert.fail(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>);
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' != 'b'</span>
assert.fail(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-literal">undefined</span>, <span class="hljs-string">'>'</span>);
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 1 > 2</span>
assert.fail(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'fail'</span>);
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: fail</span>
assert.fail(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-string">'whoops'</span>, <span class="hljs-string">'>'</span>);
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: whoops</span>
assert.fail(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-keyword">new</span> <span class="hljs-built_in">TypeError</span>(<span class="hljs-string">'need array'</span>));
<span class="hljs-comment">// TypeError: need array</span></code></pre>
<p>In the last three cases <code>actual</code>, <code>expected</code>, and <code>operator</code> have no
influence on the error message.</p>
<p>Example use of <code>stackStartFn</code> for truncating the exception's stacktrace:</p>
<pre><code class="language-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">suppressFrame</span>(<span class="hljs-params"></span>) </span>{
assert.fail(<span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-literal">undefined</span>, <span class="hljs-string">'!=='</span>, suppressFrame);
}
suppressFrame();
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: 'a' !== 'b'</span>
<span class="hljs-comment">// at repl:1:1</span>
<span class="hljs-comment">// at ContextifyScript.Script.runInThisContext (vm.js:44:33)</span>
<span class="hljs-comment">// ...</span></code></pre>
<h2>assert.ifError(value)<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L709">[src]</a><span><a class="mark" href="#assert_assert_iferror_value" id="assert_assert_iferror_value">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>Instead of throwing the original error it is now wrapped into an <code>AssertionError</code> that contains the full stack trace.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>Value may now only be <code>undefined</code> or <code>null</code>. Before all falsy values were handled the same as <code>null</code> and did not throw.</p></td></tr>
<tr><td>v0.1.97</td>
<td><p><span>Added in: v0.1.97</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
</ul>
<p>Throws <code>value</code> if <code>value</code> is not <code>undefined</code> or <code>null</code>. This is useful when
testing the <code>error</code> argument in callbacks. The stack trace contains all frames
from the error passed to <code>ifError()</code> including the potential new frames for
<code>ifError()</code> itself.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).strict;
assert.ifError(<span class="hljs-literal">null</span>);
<span class="hljs-comment">// OK</span>
assert.ifError(<span class="hljs-number">0</span>);
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0</span>
assert.ifError(<span class="hljs-string">'error'</span>);
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'</span>
assert.ifError(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>());
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: Error</span>
<span class="hljs-comment">// Create some random error frames.</span>
<span class="hljs-keyword">let</span> err;
(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">errorFrame</span>(<span class="hljs-params"></span>) </span>{
err = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'test error'</span>);
})();
(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ifErrorFrame</span>(<span class="hljs-params"></span>) </span>{
assert.ifError(err);
})();
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: test error</span>
<span class="hljs-comment">// at ifErrorFrame</span>
<span class="hljs-comment">// at errorFrame</span></code></pre>
<h2>assert.notDeepEqual(actual, expected[, message])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L407">[src]</a><span><a class="mark" href="#assert_assert_notdeepequal_actual_expected_message" id="assert_assert_notdeepequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>Error</code> names and messages are now properly compared</p></td></tr>
<tr><td>v8.0.0</td>
<td><p>The <code>Set</code> and <code>Map</code> content is also compared</p></td></tr>
<tr><td>v6.4.0, v4.7.1</td>
<td><p>Typed array slices are handled correctly now.</p></td></tr>
<tr><td>v6.1.0, v4.5.0</td>
<td><p>Objects with circular references can be used as inputs now.</p></td></tr>
<tr><td>v5.10.1, v4.4.3</td>
<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p><strong>Strict mode</strong></p>
<p>An alias of <a href="#assert_assert_notdeepstrictequal_actual_expected_message"><code>assert.notDeepStrictEqual()</code></a>.</p>
<p><strong>Legacy mode</strong></p>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#assert_assert_notdeepstrictequal_actual_expected_message"><code>assert.notDeepStrictEqual()</code></a> instead.</div><p></p>
<p>Tests for any deep inequality. Opposite of <a href="#assert_assert_deepequal_actual_expected_message"><code>assert.deepEqual()</code></a>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>);
<span class="hljs-keyword">const</span> obj1 = {
<span class="hljs-attr">a</span>: {
<span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
}
};
<span class="hljs-keyword">const</span> obj2 = {
<span class="hljs-attr">a</span>: {
<span class="hljs-attr">b</span>: <span class="hljs-number">2</span>
}
};
<span class="hljs-keyword">const</span> obj3 = {
<span class="hljs-attr">a</span>: {
<span class="hljs-attr">b</span>: <span class="hljs-number">1</span>
}
};
<span class="hljs-keyword">const</span> obj4 = <span class="hljs-built_in">Object</span>.create(obj1);
assert.notDeepEqual(obj1, obj1);
<span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span>
assert.notDeepEqual(obj1, obj2);
<span class="hljs-comment">// OK</span>
assert.notDeepEqual(obj1, obj3);
<span class="hljs-comment">// AssertionError: { a: { b: 1 } } notDeepEqual { a: { b: 1 } }</span>
assert.notDeepEqual(obj1, obj4);
<span class="hljs-comment">// OK</span></code></pre>
<p>If the values are deeply equal, an <code>AssertionError</code> is thrown with a <code>message</code>
property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
parameter is undefined, a default error message is assigned. If the <code>message</code>
parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<code>AssertionError</code>.</p>
<h2>assert.notDeepStrictEqual(actual, expected[, message])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L435">[src]</a><span><a class="mark" href="#assert_assert_notdeepstrictequal_actual_expected_message" id="assert_assert_notdeepstrictequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>-0</code> and <code>+0</code> are not considered equal anymore.</p></td></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>NaN</code> is now compared using the <a href="https://tc39.github.io/ecma262/#sec-samevaluezero">SameValueZero</a> comparison.</p></td></tr>
<tr><td>v9.0.0</td>
<td><p>The <code>Error</code> names and messages are now properly compared</p></td></tr>
<tr><td>v8.0.0</td>
<td><p>The <code>Set</code> and <code>Map</code> content is also compared</p></td></tr>
<tr><td>v6.4.0, v4.7.1</td>
<td><p>Typed array slices are handled correctly now.</p></td></tr>
<tr><td>v6.1.0</td>
<td><p>Objects with circular references can be used as inputs now.</p></td></tr>
<tr><td>v5.10.1, v4.4.3</td>
<td><p>Handle non-<code>Uint8Array</code> typed arrays correctly.</p></td></tr>
<tr><td>v1.2.0</td>
<td><p><span>Added in: v1.2.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p>Tests for deep strict inequality. Opposite of <a href="#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).strict;
assert.notDeepStrictEqual({ <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> }, { <span class="hljs-attr">a</span>: <span class="hljs-string">'1'</span> });
<span class="hljs-comment">// OK</span></code></pre>
<p>If the values are deeply and strictly equal, an <code>AssertionError</code> is thrown with
a <code>message</code> property set equal to the value of the <code>message</code> parameter. If the
<code>message</code> parameter is undefined, a default error message is assigned. If the
<code>message</code> parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown
instead of the <code>AssertionError</code>.</p>
<h2>assert.notEqual(actual, expected[, message])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L379">[src]</a><span><a class="mark" href="#assert_assert_notequal_actual_expected_message" id="assert_assert_notequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.1.21</span>
</div>
<ul>
<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p><strong>Strict mode</strong></p>
<p>An alias of <a href="#assert_assert_notstrictequal_actual_expected_message"><code>assert.notStrictEqual()</code></a>.</p>
<p><strong>Legacy mode</strong></p>
<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#assert_assert_notstrictequal_actual_expected_message"><code>assert.notStrictEqual()</code></a> instead.</div><p></p>
<p>Tests shallow, coercive inequality with the <a href="https://tc39.github.io/ecma262/#sec-abstract-equality-comparison">Abstract Equality Comparison</a>
( <code>!=</code> ).</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>);
assert.notEqual(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
<span class="hljs-comment">// OK</span>
assert.notEqual(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
<span class="hljs-comment">// AssertionError: 1 != 1</span>
assert.notEqual(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>);
<span class="hljs-comment">// AssertionError: 1 != '1'</span></code></pre>
<p>If the values are equal, an <code>AssertionError</code> is thrown with a <code>message</code> property
set equal to the value of the <code>message</code> parameter. If the <code>message</code> parameter is
undefined, a default error message is assigned. If the <code>message</code> parameter is an
instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<code>AssertionError</code>.</p>
<h2>assert.notStrictEqual(actual, expected[, message])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L460">[src]</a><span><a class="mark" href="#assert_assert_notstrictequal_actual_expected_message" id="assert_assert_notstrictequal_actual_expected_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>Used comparison changed from Strict Equality to <code>Object.is()</code></p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>actual</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>expected</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p>Tests strict inequality between the <code>actual</code> and <code>expected</code> parameters as
determined by the <a href="https://tc39.github.io/ecma262/#sec-samevalue">SameValue Comparison</a>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).strict;
assert.notStrictEqual(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
<span class="hljs-comment">// OK</span>
assert.notStrictEqual(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
<span class="hljs-comment">// AssertionError [ERR_ASSERTION]: Expected "actual" to be strictly unequal to:</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// 1</span>
assert.notStrictEqual(<span class="hljs-number">1</span>, <span class="hljs-string">'1'</span>);
<span class="hljs-comment">// OK</span></code></pre>
<p>If the values are strictly equal, an <code>AssertionError</code> is thrown with a <code>message</code>
property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
parameter is undefined, a default error message is assigned. If the <code>message</code>
parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<code>AssertionError</code>.</p>
<h2>assert.ok(value[, message])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/assert.js#L357">[src]</a><span><a class="mark" href="#assert_assert_ok_value_message" id="assert_assert_ok_value_message">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>assert.ok()</code> (no arguments) will now use a predefined error message.</p></td></tr>
<tr><td>v0.1.21</td>
<td><p><span>Added in: v0.1.21</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>message</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li>
</ul>
<p>Tests if <code>value</code> is truthy. It is equivalent to
<code>assert.equal(!!value, true, message)</code>.</p>
<p>If <code>value</code> is not truthy, an <code>AssertionError</code> is thrown with a <code>message</code>
property set equal to the value of the <code>message</code> parameter. If the <code>message</code>
parameter is <code>undefined</code>, a default error message is assigned. If the <code>message</code>
parameter is an instance of an <a href="errors.html#errors_class_error"><code>Error</code></a> then it will be thrown instead of the
<code>AssertionError</code>.
If no arguments are passed in at all <code>message</code> will be set to the string:
<code>'No value argument passed to `assert.ok()`'</code>.</p>
<p>Be aware that in the <code>repl</code> the error message will be different to the one
thrown in a file! See below for further details.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>).strict;
assert.ok(<span class="hljs-literal">true</span>);
<span class="hljs-comment">// OK</span>
assert.ok(<span class="hljs-number">1</span>);
<span class="hljs-comment">// OK</span>
assert.ok();
<span class="hljs-comment">// AssertionError: No value argument passed to `assert.ok()`</span>
assert.ok(<span class="hljs-literal">false</span>, <span class="hljs-string">'it\'s false'</span>);
<span class="hljs-comment">// AssertionError: it's false</span>
<span class="hljs-comment">// In the repl:</span>
assert.ok(<span class="hljs-keyword">typeof</span> <span class="hljs-number">123</span> === <span class="hljs-string">'string'</span>);
<span class="hljs-comment">// AssertionError: false == true</span>
<span class="hljs-comment">// In a file (e.g. test.js):</span>
assert.ok(<span class="hljs-keyword">typeof</span> <span class="hljs-number">123</span> === <span class="hljs-string">'string'</span>);
<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// assert.ok(typeof 123 === 'string')</span>
assert.ok(<span class="hljs-literal">false</span>);
<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// assert.ok(false)</span>
assert.ok(<span class="hljs-number">0</span>);
<span class="hljs-comment">// AssertionError: The expression evaluated to a falsy value:</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// assert.ok(0)</span>
<span class="hljs-comment">// Using `assert()` works the same:</span>