| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5a78c6c commit bae5de1
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,57 @@ | |||
| 7 | 7 | The `assert` module provides a simple set of assertion tests that can be used to | |
| 8 | 8 | test invariants. | |
| 9 | 9 | ||
| 10 | + A `strict` and a `legacy` mode exist, while it is recommended to only use | ||
| 11 | + [`strict mode`][]. | ||
| 12 | + | ||
| 13 | + For more information about the used equality comparisons see | ||
| 14 | + [MDN's guide on equality comparisons and sameness][mdn-equality-guide]. | ||
| 15 | + | ||
| 16 | + ## Strict mode | ||
| 17 | + <!-- YAML | ||
| 18 | + added: REPLACEME | ||
| 19 | + changes: | ||
| 20 | + - version: REPLACEME | ||
| 21 | + pr-url: https://github.com/nodejs/node/pull/17002 | ||
| 22 | + description: Added strict mode to the assert module. | ||
| 23 | + --> | ||
| 24 | + | ||
| 25 | + When using the `strict mode`, any `assert` function will use the equality used in | ||
| 26 | + the strict function mode. So [`assert.deepEqual()`][] will, for example, work the | ||
| 27 | + same as [`assert.deepStrictEqual()`][]. | ||
| 28 | + | ||
| 29 | + It can be accessed using: | ||
| 30 | + | ||
| 31 | + ```js | ||
| 32 | + const assert = require('assert').strict; | ||
| 33 | + ``` | ||
| 34 | + | ||
| 35 | + ## Legacy mode | ||
| 36 | + | ||
| 37 | + > Stability: 0 - Deprecated: Use strict mode instead. | ||
| 38 | + | ||
| 39 | + When accessing `assert` directly instead of using the `strict` property, the | ||
| 40 | + [Abstract Equality Comparison][] will be used for any function without a | ||
| 41 | + "strict" in its name (e.g. [`assert.deepEqual()`][]). | ||
| 42 | + | ||
| 43 | + It can be accessed using: | ||
| 44 | + | ||
| 45 | + ```js | ||
| 46 | + const assert = require('assert'); | ||
| 47 | + ``` | ||
| 48 | + | ||
| 49 | + It is recommended to use the [`strict mode`][] instead as the | ||
| 50 | + [Abstract Equality Comparison][] can often have surprising results. Especially | ||
| 51 | + in case of [`assert.deepEqual()`][] as the used comparison rules there are very | ||
| 52 | + lax. | ||
| 53 | + | ||
| 54 | + E.g. | ||
| 55 | + | ||
| 56 | + ```js | ||
| 57 | + // WARNING: This does not throw an AssertionError! | ||
| 58 | + assert.deepEqual(/a/gi, new Date()); | ||
| 59 | + ``` | ||
| 60 | + | ||
| 10 | 61 | ## assert(value[, message]) | |
| 11 | 62 | <!-- YAML | |
| 12 | 63 | added: v0.5.9 | |
@@ -40,6 +91,14 @@ changes: | |||
| 40 | 91 | * `expected` {any} | |
| 41 | 92 | * `message` {any} | |
| 42 | 93 | ||
| 94 | + **Strict mode** | ||
| 95 | + | ||
| 96 | + An alias of [`assert.deepStrictEqual()`][]. | ||
| 97 | + | ||
| 98 | + **Legacy mode** | ||
| 99 | + | ||
| 100 | + > Stability: 0 - Deprecated: Use [`assert.deepStrictEqual()`][] instead. | ||
| 101 | + | ||
| 43 | 102 | Tests for deep equality between the `actual` and `expected` parameters. | |
| 44 | 103 | Primitive values are compared with the [Abstract Equality Comparison][] | |
| 45 | 104 | ( `==` ). | |
@@ -146,7 +205,7 @@ are recursively evaluated also by the following rules. | |||
| 146 | 205 | [`Object.is()`][]. | |
| 147 | 206 | * [Type tags][Object.prototype.toString()] of objects should be the same. | |
| 148 | 207 | * [`[[Prototype]]`][prototype-spec] of objects are compared using | |
| 149 | - the [Strict Equality Comparison][] too. | ||
| 208 | + the [Strict Equality Comparison][]. | ||
| 150 | 209 | * Only [enumerable "own" properties][] are considered. | |
| 151 | 210 | * [`Error`][] names and messages are always compared, even if these are not | |
| 152 | 211 | enumerable properties. | |
@@ -158,7 +217,7 @@ are recursively evaluated also by the following rules. | |||
| 158 | 217 | reference. | |
| 159 | 218 | ||
| 160 | 219 | ```js | |
| 161 | - const assert = require('assert'); | ||
| 220 | + const assert = require('assert').strict; | ||
| 162 | 221 | ||
| 163 | 222 | assert.deepStrictEqual({ a: 1 }, { a: '1' }); | |
| 164 | 223 | // AssertionError: { a: 1 } deepStrictEqual { a: '1' } | |
@@ -278,6 +337,14 @@ added: v0.1.21 | |||
| 278 | 337 | * `expected` {any} | |
| 279 | 338 | * `message` {any} | |
| 280 | 339 | ||
| 340 | + **Strict mode** | ||
| 341 | + | ||
| 342 | + An alias of [`assert.strictEqual()`][]. | ||
| 343 | + | ||
| 344 | + **Legacy mode** | ||
| 345 | + | ||
| 346 | + > Stability: 0 - Deprecated: Use [`assert.strictEqual()`][] instead. | ||
| 347 | + | ||
| 281 | 348 | Tests shallow, coercive equality between the `actual` and `expected` parameters | |
| 282 | 349 | using the [Abstract Equality Comparison][] ( `==` ). | |
| 283 | 350 | ||
@@ -324,7 +391,7 @@ all stack frames above that function will be removed from stacktrace (see | |||
| 324 | 391 | `Failed` will be used. | |
| 325 | 392 | ||
| 326 | 393 | ```js | |
| 327 | - const assert = require('assert'); | ||
| 394 | + const assert = require('assert').strict; | ||
| 328 | 395 | ||
| 329 | 396 | assert.fail(1, 2, undefined, '>'); | |
| 330 | 397 | // AssertionError [ERR_ASSERTION]: 1 > 2 | |
@@ -375,7 +442,7 @@ Throws `value` if `value` is truthy. This is useful when testing the `error` | |||
| 375 | 442 | argument in callbacks. | |
| 376 | 443 | ||
| 377 | 444 | ```js | |
| 378 | - const assert = require('assert'); | ||
| 445 | + const assert = require('assert').strict; | ||
| 379 | 446 | ||
| 380 | 447 | assert.ifError(null); | |
| 381 | 448 | // OK | |
@@ -413,6 +480,14 @@ changes: | |||
| 413 | 480 | * `expected` {any} | |
| 414 | 481 | * `message` {any} | |
| 415 | 482 | ||
| 483 | + **Strict mode** | ||
| 484 | + | ||
| 485 | + An alias of [`assert.notDeepStrictEqual()`][]. | ||
| 486 | + | ||
| 487 | + **Legacy mode** | ||
| 488 | + | ||
| 489 | + > Stability: 0 - Deprecated: Use [`assert.notDeepStrictEqual()`][] instead. | ||
| 490 | + | ||
| 416 | 491 | Tests for any deep inequality. Opposite of [`assert.deepEqual()`][]. | |
| 417 | 492 | ||
| 418 | 493 | ```js | |
@@ -489,7 +564,7 @@ changes: | |||
| 489 | 564 | Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual()`][]. | |
| 490 | 565 | ||
| 491 | 566 | ```js | |
| 492 | - const assert = require('assert'); | ||
| 567 | + const assert = require('assert').strict; | ||
| 493 | 568 | ||
| 494 | 569 | assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); | |
| 495 | 570 | // OK | |
@@ -509,6 +584,14 @@ added: v0.1.21 | |||
| 509 | 584 | * `expected` {any} | |
| 510 | 585 | * `message` {any} | |
| 511 | 586 | ||
| 587 | + **Strict mode** | ||
| 588 | + | ||
| 589 | + An alias of [`assert.notStrictEqual()`][]. | ||
| 590 | + | ||
| 591 | + **Legacy mode** | ||
| 592 | + | ||
| 593 | + > Stability: 0 - Deprecated: Use [`assert.notStrictEqual()`][] instead. | ||
| 594 | + | ||
| 512 | 595 | Tests shallow, coercive inequality with the [Abstract Equality Comparison][] | |
| 513 | 596 | ( `!=` ). | |
| 514 | 597 | ||
@@ -543,7 +626,7 @@ Tests strict inequality between the `actual` and `expected` parameters as | |||
| 543 | 626 | determined by the [SameValue Comparison][]. | |
| 544 | 627 | ||
| 545 | 628 | ```js | |
| 546 | - const assert = require('assert'); | ||
| 629 | + const assert = require('assert').strict; | ||
| 547 | 630 | ||
| 548 | 631 | assert.notStrictEqual(1, 2); | |
| 549 | 632 | // OK | |
@@ -578,7 +661,7 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the | |||
| 578 | 661 | `AssertionError`. | |
| 579 | 662 | ||
| 580 | 663 | ```js | |
| 581 | - const assert = require('assert'); | ||
| 664 | + const assert = require('assert').strict; | ||
| 582 | 665 | ||
| 583 | 666 | assert.ok(true); | |
| 584 | 667 | // OK | |
@@ -604,7 +687,7 @@ Tests strict equality between the `actual` and `expected` parameters as | |||
| 604 | 687 | determined by the [SameValue Comparison][]. | |
| 605 | 688 | ||
| 606 | 689 | ```js | |
| 607 | - const assert = require('assert'); | ||
| 690 | + const assert = require('assert').strict; | ||
| 608 | 691 | ||
| 609 | 692 | assert.strictEqual(1, 2); | |
| 610 | 693 | // AssertionError: 1 === 2 | |
@@ -728,8 +811,12 @@ For more information, see | |||
| 728 | 811 | [`TypeError`]: errors.html#errors_class_typeerror | |
| 729 | 812 | [`assert.deepEqual()`]: #assert_assert_deepequal_actual_expected_message | |
| 730 | 813 | [`assert.deepStrictEqual()`]: #assert_assert_deepstrictequal_actual_expected_message | |
| 814 | + [`assert.notDeepStrictEqual()`]: #assert_assert_notdeepstrictequal_actual_expected_message | ||
| 815 | + [`assert.notStrictEqual()`]: #assert_assert_notstrictequal_actual_expected_message | ||
| 731 | 816 | [`assert.ok()`]: #assert_assert_ok_value_message | |
| 817 | + [`assert.strictEqual()`]: #assert_assert_strictequal_actual_expected_message | ||
| 732 | 818 | [`assert.throws()`]: #assert_assert_throws_block_error_message | |
| 819 | + [`strict mode`]: #assert_strict_mode | ||
| 733 | 820 | [Abstract Equality Comparison]: https://tc39.github.io/ecma262/#sec-abstract-equality-comparison | |
| 734 | 821 | [Object.prototype.toString()]: https://tc39.github.io/ecma262/#sec-object.prototype.tostring | |
| 735 | 822 | [SameValueZero]: https://tc39.github.io/ecma262/#sec-samevaluezero | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -774,19 +774,15 @@ Type: Runtime | |||
| 774 | 774 | cause a lot of issues. See https://github.com/nodejs/node/issues/14328 for more | |
| 775 | 775 | details. | |
| 776 | 776 | ||
| 777 | - <a id="DEP0098"></a> | ||
| 778 | - ### DEP0098: AsyncHooks Embedder AsyncResource.emit{Before,After} APIs | ||
| 777 | + <a id="DEP0089"></a> | ||
| 778 | + ### DEP0089: require('assert') | ||
| 779 | 779 | ||
| 780 | - Type: Runtime | ||
| 781 | - | ||
| 782 | - The embedded API provided by AsyncHooks exposes emit{Before,After} methods | ||
| 783 | - which are very easy to use incorrectly which can lead to unrecoverable errors. | ||
| 780 | + Type: Documentation-only | ||
| 784 | 781 | ||
| 785 | - Use [`asyncResource.runInAsyncScope()`][] API instead which provides a much | ||
| 786 | - safer, and more convenient, alternative. See | ||
| 787 | - https://github.com/nodejs/node/pull/18513 for more details. | ||
| 782 | + Importing assert directly is not recommended as the exposed functions will use | ||
| 783 | + loose equality checks. Use `require('assert').strict` instead. The API is the | ||
| 784 | + same as the legacy assert but it will always use strict equality checks. | ||
| 788 | 785 | ||
| 789 | - [`--pending-deprecation`]: cli.html#cli_pending_deprecation | ||
| 790 | 786 | [`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size | |
| 791 | 787 | [`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array | |
| 792 | 788 | [`Buffer.from(buffer)`]: buffer.html#buffer_class_method_buffer_from_buffer | |
@@ -796,7 +792,6 @@ https://github.com/nodejs/node/pull/18513 for more details. | |||
| 796 | 792 | [`Server.getConnections()`]: net.html#net_server_getconnections_callback | |
| 797 | 793 | [`Server.listen({fd: <number>})`]: net.html#net_server_listen_handle_backlog_callback | |
| 798 | 794 | [`SlowBuffer`]: buffer.html#buffer_class_slowbuffer | |
| 799 | - [`asyncResource.runInAsyncScope()`]: async_hooks.html#async_hooks_asyncresource_runinasyncscope_fn_thisarg_args | ||
| 800 | 795 | [`child_process`]: child_process.html | |
| 801 | 796 | [`console.error()`]: console.html#console_console_error_data_args | |
| 802 | 797 | [`console.log()`]: console.html#console_console_log_data_args | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -210,3 +210,15 @@ assert.doesNotThrow = function doesNotThrow(block, error, message) { | |||
| 210 | 210 | }; | |
| 211 | 211 | ||
| 212 | 212 | assert.ifError = function ifError(err) { if (err) throw err; }; | |
| 213 | + | ||
| 214 | + // Expose a strict only variant of assert | ||
| 215 | + function strict(value, message) { | ||
| 216 | + if (!value) innerFail(value, true, message, '==', strict); | ||
| 217 | + } | ||
| 218 | + assert.strict = Object.assign(strict, assert, { | ||
| 219 | + equal: assert.strictEqual, | ||
| 220 | + deepEqual: assert.deepStrictEqual, | ||
| 221 | + notEqual: assert.notStrictEqual, | ||
| 222 | + notDeepEqual: assert.notDeepStrictEqual | ||
| 223 | + }); | ||
| 224 | + assert.strict.strict = assert.strict; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -761,3 +761,22 @@ common.expectsError( | |||
| 761 | 761 | message: /^'Error: foo' === 'Error: foobar'$/ | |
| 762 | 762 | } | |
| 763 | 763 | ); | |
| 764 | + | ||
| 765 | + // Test strict assert | ||
| 766 | + { | ||
| 767 | + const a = require('assert'); | ||
| 768 | + const assert = require('assert').strict; | ||
| 769 | + /* eslint-disable no-restricted-properties */ | ||
| 770 | + assert.throws(() => assert.equal(1, true), assert.AssertionError); | ||
| 771 | + assert.notEqual(0, false); | ||
| 772 | + assert.throws(() => assert.deepEqual(1, true), assert.AssertionError); | ||
| 773 | + assert.notDeepEqual(0, false); | ||
| 774 | + assert.equal(assert.strict, assert.strict.strict); | ||
| 775 | + assert.equal(assert.equal, assert.strictEqual); | ||
| 776 | + assert.equal(assert.deepEqual, assert.deepStrictEqual); | ||
| 777 | + assert.equal(assert.notEqual, assert.notStrictEqual); | ||
| 778 | + assert.equal(assert.notDeepEqual, assert.notDeepStrictEqual); | ||
| 779 | + assert.equal(Object.keys(assert).length, Object.keys(a).length); | ||
| 780 | + /* eslint-enable no-restricted-properties */ | ||
| 781 | + assert(7); | ||
| 782 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments