Skip to content

Commit 04e92f0

Browse files
puskin94synapse
andcommitted
assert: adds deepMatch, deepMatchStrict, includes, includesStrict
Fixes: #50399 Co-Authored-By: Cristian Barlutiu <cristian.barlutiu@gmail.com>
1 parent beabcec commit 04e92f0

File tree

3 files changed

+1001
-1
lines changed

3 files changed

+1001
-1
lines changed

doc/api/assert.md

+235
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,237 @@ assert.throws(throwingFirst, /Second$/);
25482548
Due to the confusing error-prone notation, avoid a string as the second
25492549
argument.
25502550

2551+
## `assert.deepMatch(actual, expected[, message])`
2552+
2553+
<!-- YAML
2554+
added: REPLACEME
2555+
-->
2556+
2557+
* `actual` {any}
2558+
* `expected` {any}
2559+
* `message` {string|Error}
2560+
2561+
[`assert.deepMatch()`][] evaluates the equivalence between the `actual` and `expected` parameters by
2562+
performing a deep comparison. This function ensures that all properties defined
2563+
in the `expected` parameter match those in the `actual` parameter in
2564+
both value and type, allowing type coercion.
2565+
2566+
```mjs
2567+
import assert from 'node:assert';
2568+
2569+
assert.deepMatch({ a: 1, b: '2' }, { a: 1, b: 2 });
2570+
// OK
2571+
2572+
assert.deepMatch({ a: 1, b: '2', c: 3 }, { a: 1, b: 2 });
2573+
// OK
2574+
2575+
assert.deepMatch({ a: { b: { c: '1' } } }, { a: { b: { c: 1 } } });
2576+
// OK
2577+
2578+
assert.deepMatch({ a: 1 }, { a: 1, b: 2 });
2579+
// AssertionError
2580+
2581+
assert.deepMatch({ a: 1, b: true }, { a: 1, b: 'true' });
2582+
// AssertionError
2583+
2584+
assert.deepMatch({ a: { b: 2 } }, { a: { b: 2, c: 3 } });
2585+
// AssertionError
2586+
```
2587+
2588+
```cjs
2589+
const assert = require('node:assert');
2590+
2591+
assert.deepMatch({ a: 1, b: '2' }, { a: 1, b: 2 });
2592+
// OK
2593+
2594+
assert.deepMatch({ a: 1, b: '2', c: 3 }, { a: 1, b: 2 });
2595+
// OK
2596+
2597+
assert.deepMatch({ a: { b: { c: '1' } } }, { a: { b: { c: 1 } } });
2598+
// OK
2599+
2600+
assert.deepMatch({ a: 1 }, { a: 1, b: 2 });
2601+
// AssertionError: Expected key b
2602+
2603+
assert.deepMatch({ a: 1, b: true }, { a: 1, b: 'true' });
2604+
// AssertionError
2605+
2606+
assert.deepMatch({ a: { b: 2, d: 4 } }, { a: { b: 2, c: 3 } });
2607+
// AssertionError: Expected key c
2608+
```
2609+
2610+
If the values or keys are not equal in the `expected` parameter, an [`AssertionError`][] is thrown with a `message`
2611+
property set equal to the value of the `message` parameter. If the `message`
2612+
parameter is undefined, a default error message is assigned. If the `message`
2613+
parameter is an instance of an [`Error`][] then it will be thrown instead of the
2614+
`AssertionError`.
2615+
2616+
## `assert.deepMatchStrict(actual, expected[, message])`
2617+
2618+
<!-- YAML
2619+
added: REPLACEME
2620+
-->
2621+
2622+
* `actual` {any}
2623+
* `expected` {any}
2624+
* `message` {string|Error}
2625+
2626+
[`assert.deepMatchStrict()`][] Assesses the equivalence between the `actual` and `expected` parameters through a
2627+
deep comparison, ensuring that all properties in the `expected` parameter are
2628+
present in the `actual` parameter with equivalent values, not allowing type coercion.
2629+
2630+
```mjs
2631+
import assert from 'node:assert';
2632+
2633+
assert.deepMatchStrict({ a: 1, b: 2 }, { a: 1, b: 2 });
2634+
// OK
2635+
2636+
assert.deepMatchStrict({ a: { b: { c: 1 } } }, { a: { b: { c: 1 } } });
2637+
// OK
2638+
2639+
assert.deepMatchStrict({ a: 1, b: 2, c: 3 }, { a: 1, b: 2 });
2640+
// OK
2641+
2642+
assert.deepMatchStrict({ a: 1 }, { a: 1, b: 2 });
2643+
// AssertionError
2644+
2645+
assert.deepMatchStrict({ a: 1, b: '2' }, { a: 1, b: 2 });
2646+
// AssertionError
2647+
2648+
assert.deepMatchStrict({ a: { b: 2 } }, { a: { b: '2' } });
2649+
// AssertionError
2650+
```
2651+
2652+
```cjs
2653+
const assert = require('node:assert');
2654+
2655+
assert.deepMatchStrict({ a: 1, b: 2 }, { a: 1, b: 2 });
2656+
// OK
2657+
2658+
assert.deepMatchStrict({ a: { b: { c: 1 } } }, { a: { b: { c: 1 } } });
2659+
// OK
2660+
2661+
assert.deepMatchStrict({ a: 1, b: 2, c: 3 }, { a: 1, b: 2 });
2662+
// OK
2663+
2664+
assert.deepMatchStrict({ a: 1 }, { a: 1, b: 2 });
2665+
// AssertionError
2666+
2667+
assert.deepMatchStrict({ a: 1, b: '2' }, { a: 1, b: 2 });
2668+
// AssertionError
2669+
2670+
assert.deepMatchStrict({ a: { b: 2 } }, { a: { b: '2' } });
2671+
// AssertionError
2672+
```
2673+
2674+
## `assert.includes(actual, expected[, message])`
2675+
2676+
<!-- YAML
2677+
added: REPLACEME
2678+
-->
2679+
2680+
* `actual` {Array | string}
2681+
* `expected` {Array | string}
2682+
* `message` {string|Error}
2683+
2684+
[`assert.includes()`][] compares the `actual` and `expected` parameters to determine if the `expected`
2685+
parameter is included in the `actual` parameter; the comparison is done with type coercion.
2686+
The `actual` and the `expected` parameters can be
2687+
either an array or a string. If the `actual` parameter is an array,
2688+
the `expected` parameter must be an array and vice versa for strings.
2689+
2690+
```mjs
2691+
import assert from 'node:assert';
2692+
2693+
assert.includes([1, 2, 3], ['2', 3]);
2694+
// OK
2695+
2696+
assert.includes('Hello World!', 'World');
2697+
// OK
2698+
2699+
assert.includes([1, 2, 3], [2, 4]);
2700+
// AssertionError
2701+
2702+
assert.includes('Hello World!', 'Node.js');
2703+
// AssertionError
2704+
```
2705+
2706+
```cjs
2707+
const assert = require('node:assert');
2708+
2709+
assert.includes([1, 2, 3], [2, 3]);
2710+
// OK
2711+
2712+
assert.includes('Hello World!', 'World');
2713+
// OK
2714+
2715+
assert.includes([1, 2, 3], [2, 4]);
2716+
// AssertionError
2717+
2718+
assert.includes('Hello World!', 'Node.js');
2719+
// AssertionError
2720+
```
2721+
2722+
If the assertion fails, an [`AssertionError`][] is thrown with a `message`
2723+
property set equal to the value of the `message` parameter. If the `message`
2724+
parameter is undefined, a default error message is assigned. If the `message`
2725+
parameter is an instance of an [`Error`][] then it will be thrown instead of the
2726+
`AssertionError`.
2727+
2728+
## `assert.includesStrict(actual, expected[, message])`
2729+
2730+
<!-- YAML
2731+
added: REPLACEME
2732+
-->
2733+
2734+
* `actual` {Array | string}
2735+
* `expected` {Array | string}
2736+
* `message` {string|Error}
2737+
2738+
[`assert.includesStrict()`][] compares the `actual` and `expected` parameters to determine if the `expected`
2739+
parameter is included in the `actual` parameter; the comparison is done without type coercion.
2740+
The `actual` and the `expected` parameters can be
2741+
either an array or a string. If the `actual` parameter is an array,
2742+
the `expected` parameter must be an array and vice versa for strings.
2743+
2744+
```mjs
2745+
import assert from 'node:assert';
2746+
2747+
assert.includesStrict([1, 2, 3], [2, 3]);
2748+
// OK
2749+
2750+
assert.includesStrict('Hello World!', 'World');
2751+
// OK
2752+
2753+
assert.includesStrict([1, 2, 3], [2, 4]);
2754+
// AssertionError
2755+
2756+
assert.includesStrict('Hello World!', 'Node.js');
2757+
// AssertionError
2758+
```
2759+
2760+
```cjs
2761+
const assert = require('node:assert');
2762+
2763+
assert.includesStrict([1, 2, 3], [2, 3]);
2764+
// OK
2765+
2766+
assert.includesStrict('Hello World!', 'World');
2767+
// OK
2768+
2769+
assert.includesStrict([1, 2, 3], [2, 4]);
2770+
// AssertionError
2771+
2772+
assert.includesStrict('Hello World!', 'Node.js');
2773+
// AssertionError
2774+
```
2775+
2776+
If the assertion fails, an [`AssertionError`][] is thrown with a `message`
2777+
property set equal to the value of the `message` parameter. If the `message`
2778+
parameter is undefined, a default error message is assigned. If the `message`
2779+
parameter is an instance of an [`Error`][] then it will be thrown instead of the
2780+
`AssertionError`.
2781+
25512782
[Object wrappers]: https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript
25522783
[Object.prototype.toString()]: https://tc39.github.io/ecma262/#sec-object.prototype.tostring
25532784
[`!=` operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Inequality
@@ -2568,9 +2799,13 @@ argument.
25682799
[`WeakMap`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
25692800
[`WeakSet`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet
25702801
[`assert.deepEqual()`]: #assertdeepequalactual-expected-message
2802+
[`assert.deepMatch()`]: #assertdeepmatchactual-expected-message
2803+
[`assert.deepMatchStrict()`]: #assertdeepmatchstrictactual-expected-message
25712804
[`assert.deepStrictEqual()`]: #assertdeepstrictequalactual-expected-message
25722805
[`assert.doesNotThrow()`]: #assertdoesnotthrowfn-error-message
25732806
[`assert.equal()`]: #assertequalactual-expected-message
2807+
[`assert.includes()`]: #assertincludesactual-expected-message
2808+
[`assert.includesStrict()`]: #assertincludesstrictactual-expected-message
25742809
[`assert.notDeepEqual()`]: #assertnotdeepequalactual-expected-message
25752810
[`assert.notDeepStrictEqual()`]: #assertnotdeepstrictequalactual-expected-message
25762811
[`assert.notEqual()`]: #assertnotequalactual-expected-message

0 commit comments

Comments
 (0)