@@ -39,6 +39,27 @@ strict methods. For example, [`assert.deepEqual()`][] will behave like |
39 | 39 | In strict assertion mode, error messages for objects display a diff. In legacy |
40 | 40 | assertion mode, error messages for objects display the objects, often truncated. |
41 | 41 |
|
| 42 | +### Message parameter semantics |
| 43 | + |
| 44 | +For assertion methods that accept an optional `message` parameter, the message |
| 45 | +may be provided in one of the following forms: |
| 46 | + |
| 47 | +* **string**: Used as-is. If additional arguments are supplied after the |
| 48 | + `message` string, they are treated as printf-like substitutions (see |
| 49 | + [`util.format()`][]). |
| 50 | +* **Error**: If an `Error` instance is provided as `message`, that error is |
| 51 | + thrown directly instead of an `AssertionError`. |
| 52 | +* **function**: A function of the form `(actual, expected) => string`. It is |
| 53 | + called only when the assertion fails and should return a string to be used as |
| 54 | + the error message. Non-string return values are ignored and the default |
| 55 | + message is used instead. |
| 56 | + |
| 57 | +If additional arguments are passed along with an `Error` or a function as |
| 58 | +`message`, the call is rejected with `ERR_AMBIGUOUS_ARGUMENT`. |
| 59 | + |
| 60 | +If the first item is neither a string, `Error`, nor function, `ERR_INVALID_ARG_TYPE` |
| 61 | +is thrown. |
| 62 | + |
42 | 63 | To use strict assertion mode: |
43 | 64 |
|
44 | 65 | ```mjs |
@@ -305,10 +326,14 @@ destructuring and call methods directly on the instance. |
305 | 326 |
|
306 | 327 | <!-- YAML |
307 | 328 | added: v0.5.9 |
| 329 | +changes: |
| 330 | + - version: REPLACEME |
| 331 | + pr-url: https://github.com/nodejs/node/pull/58849 |
| 332 | + description: Message may now be a `printf`-like format string or function. |
308 | 333 | --> |
309 | 334 |
|
310 | 335 | * `value` {any} The input that is checked for being truthy. |
311 | | -* `message` {string|Error} |
| 336 | +* `message` {string|Error|Function} |
312 | 337 |
|
313 | 338 | An alias of [`assert.ok()`][]. |
314 | 339 |
|
@@ -324,6 +349,9 @@ changes: |
324 | 349 | - version: v25.0.0 |
325 | 350 | pr-url: https://github.com/nodejs/node/pull/57627 |
326 | 351 | description: Invalid dates are now considered equal. |
| 352 | + - version: REPLACEME |
| 353 | + pr-url: https://github.com/nodejs/node/pull/58849 |
| 354 | + description: Message may now be a `printf`-like format string or function. |
327 | 355 | - version: v24.0.0 |
328 | 356 | pr-url: https://github.com/nodejs/node/pull/57622 |
329 | 357 | description: Recursion now stops when either side encounters a circular |
@@ -375,7 +403,7 @@ changes: |
375 | 403 |
|
376 | 404 | * `actual` {any} |
377 | 405 | * `expected` {any} |
378 | | -* `message` {string|Error} |
| 406 | +* `message` {string|Error|Function} |
379 | 407 |
|
380 | 408 | **Strict assertion mode** |
381 | 409 |
|
@@ -524,6 +552,9 @@ changes: |
524 | 552 | - version: v25.0.0 |
525 | 553 | pr-url: https://github.com/nodejs/node/pull/57627 |
526 | 554 | description: Invalid dates are now considered equal. |
| 555 | + - version: REPLACEME |
| 556 | + pr-url: https://github.com/nodejs/node/pull/58849 |
| 557 | + description: Message may now be a `printf`-like format string or function. |
527 | 558 | - version: v24.0.0 |
528 | 559 | pr-url: https://github.com/nodejs/node/pull/57622 |
529 | 560 | description: Recursion now stops when either side encounters a circular |
@@ -567,7 +598,7 @@ changes: |
567 | 598 |
|
568 | 599 | * `actual` {any} |
569 | 600 | * `expected` {any} |
570 | | -* `message` {string|Error} |
| 601 | +* `message` {string|Error|Function} |
571 | 602 |
|
572 | 603 | Tests for deep equality between the `actual` and `expected` parameters. |
573 | 604 | "Deep" equality means that the enumerable "own" properties of child objects |
@@ -829,14 +860,17 @@ added: |
829 | 860 | - v13.6.0 |
830 | 861 | - v12.16.0 |
831 | 862 | changes: |
| 863 | + - version: REPLACEME |
| 864 | + pr-url: https://github.com/nodejs/node/pull/58849 |
| 865 | + description: Message may now be a `printf`-like format string or function. |
832 | 866 | - version: v16.0.0 |
833 | 867 | pr-url: https://github.com/nodejs/node/pull/38111 |
834 | 868 | description: This API is no longer experimental. |
835 | 869 | --> |
836 | 870 |
|
837 | 871 | * `string` {string} |
838 | 872 | * `regexp` {RegExp} |
839 | | -* `message` {string|Error} |
| 873 | +* `message` {string|Error|Function} |
840 | 874 |
|
841 | 875 | Expects the `string` input not to match the regular expression. |
842 | 876 |
|
@@ -1069,6 +1103,9 @@ assert.doesNotThrow( |
1069 | 1103 | <!-- YAML |
1070 | 1104 | added: v0.1.21 |
1071 | 1105 | changes: |
| 1106 | + - version: REPLACEME |
| 1107 | + pr-url: https://github.com/nodejs/node/pull/58849 |
| 1108 | + description: Message may now be a `printf`-like format string or function. |
1072 | 1109 | - version: |
1073 | 1110 | - v16.0.0 |
1074 | 1111 | - v14.18.0 |
@@ -1083,7 +1120,7 @@ changes: |
1083 | 1120 |
|
1084 | 1121 | * `actual` {any} |
1085 | 1122 | * `expected` {any} |
1086 | | -* `message` {string|Error} |
| 1123 | +* `message` {string|Error|Function} |
1087 | 1124 |
|
1088 | 1125 | **Strict assertion mode** |
1089 | 1126 |
|
@@ -1254,14 +1291,17 @@ added: |
1254 | 1291 | - v13.6.0 |
1255 | 1292 | - v12.16.0 |
1256 | 1293 | changes: |
| 1294 | + - version: REPLACEME |
| 1295 | + pr-url: https://github.com/nodejs/node/pull/58849 |
| 1296 | + description: Message may now be a `printf`-like format string or function. |
1257 | 1297 | - version: v16.0.0 |
1258 | 1298 | pr-url: https://github.com/nodejs/node/pull/38111 |
1259 | 1299 | description: This API is no longer experimental. |
1260 | 1300 | --> |
1261 | 1301 |
|
1262 | 1302 | * `string` {string} |
1263 | 1303 | * `regexp` {RegExp} |
1264 | | -* `message` {string|Error} |
| 1304 | +* `message` {string|Error|Function} |
1265 | 1305 |
|
1266 | 1306 | Expects the `string` input to match the regular expression. |
1267 | 1307 |
|
@@ -1303,6 +1343,9 @@ instance of {Error} then it will be thrown instead of the |
1303 | 1343 | <!-- YAML |
1304 | 1344 | added: v0.1.21 |
1305 | 1345 | changes: |
| 1346 | + - version: REPLACEME |
| 1347 | + pr-url: https://github.com/nodejs/node/pull/58849 |
| 1348 | + description: Message may now be a `printf`-like format string or function. |
1306 | 1349 | - version: |
1307 | 1350 | - v16.0.0 |
1308 | 1351 | - v14.18.0 |
@@ -1338,7 +1381,7 @@ changes: |
1338 | 1381 |
|
1339 | 1382 | * `actual` {any} |
1340 | 1383 | * `expected` {any} |
1341 | | -* `message` {string|Error} |
| 1384 | +* `message` {string|Error|Function} |
1342 | 1385 |
|
1343 | 1386 | **Strict assertion mode** |
1344 | 1387 |
|
@@ -1427,6 +1470,9 @@ instead of the `AssertionError`. |
1427 | 1470 | <!-- YAML |
1428 | 1471 | added: v1.2.0 |
1429 | 1472 | changes: |
| 1473 | + - version: REPLACEME |
| 1474 | + pr-url: https://github.com/nodejs/node/pull/58849 |
| 1475 | + description: Message may now be a `printf`-like format string or function. |
1430 | 1476 | - version: v9.0.0 |
1431 | 1477 | pr-url: https://github.com/nodejs/node/pull/15398 |
1432 | 1478 | description: The `-0` and `+0` are not considered equal anymore. |
@@ -1458,7 +1504,7 @@ changes: |
1458 | 1504 |
|
1459 | 1505 | * `actual` {any} |
1460 | 1506 | * `expected` {any} |
1461 | | -* `message` {string|Error} |
| 1507 | +* `message` {string|Error|Function} |
1462 | 1508 |
|
1463 | 1509 | Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual()`][]. |
1464 | 1510 |
|
@@ -1487,6 +1533,9 @@ instead of the [`AssertionError`][]. |
1487 | 1533 | <!-- YAML |
1488 | 1534 | added: v0.1.21 |
1489 | 1535 | changes: |
| 1536 | + - version: REPLACEME |
| 1537 | + pr-url: https://github.com/nodejs/node/pull/58849 |
| 1538 | + description: Message may now be a `printf`-like format string or function. |
1490 | 1539 | - version: |
1491 | 1540 | - v16.0.0 |
1492 | 1541 | - v14.18.0 |
@@ -1501,7 +1550,7 @@ changes: |
1501 | 1550 |
|
1502 | 1551 | * `actual` {any} |
1503 | 1552 | * `expected` {any} |
1504 | | -* `message` {string|Error} |
| 1553 | +* `message` {string|Error|Function} |
1505 | 1554 |
|
1506 | 1555 | **Strict assertion mode** |
1507 | 1556 |
|
@@ -1551,14 +1600,17 @@ parameter is an instance of {Error} then it will be thrown instead of the |
1551 | 1600 | <!-- YAML |
1552 | 1601 | added: v0.1.21 |
1553 | 1602 | changes: |
| 1603 | + - version: REPLACEME |
| 1604 | + pr-url: https://github.com/nodejs/node/pull/58849 |
| 1605 | + description: Message may now be a `printf`-like format string or function. |
1554 | 1606 | - version: v10.0.0 |
1555 | 1607 | pr-url: https://github.com/nodejs/node/pull/17003 |
1556 | 1608 | description: Used comparison changed from Strict Equality to `Object.is()`. |
1557 | 1609 | --> |
1558 | 1610 |
|
1559 | 1611 | * `actual` {any} |
1560 | 1612 | * `expected` {any} |
1561 | | -* `message` {string|Error} |
| 1613 | +* `message` {string|Error|Function} |
1562 | 1614 |
|
1563 | 1615 | Tests strict inequality between the `actual` and `expected` parameters as |
1564 | 1616 | determined by [`Object.is()`][]. |
@@ -1604,14 +1656,17 @@ instead of the `AssertionError`. |
1604 | 1656 | <!-- YAML |
1605 | 1657 | added: v0.1.21 |
1606 | 1658 | changes: |
| 1659 | + - version: REPLACEME |
| 1660 | + pr-url: https://github.com/nodejs/node/pull/58849 |
| 1661 | + description: Message may now be a `printf`-like format string or function. |
1607 | 1662 | - version: v10.0.0 |
1608 | 1663 | pr-url: https://github.com/nodejs/node/pull/18319 |
1609 | 1664 | description: The `assert.ok()` (no arguments) will now use a predefined |
1610 | 1665 | error message. |
1611 | 1666 | --> |
1612 | 1667 |
|
1613 | 1668 | * `value` {any} |
1614 | | -* `message` {string|Error} |
| 1669 | +* `message` {string|Error|Function} |
1615 | 1670 |
|
1616 | 1671 | Tests if `value` is truthy. It is equivalent to |
1617 | 1672 | `assert.equal(!!value, true, message)`. |
@@ -1844,14 +1899,24 @@ argument gets considered. |
1844 | 1899 | <!-- YAML |
1845 | 1900 | added: v0.1.21 |
1846 | 1901 | changes: |
| 1902 | + - version: REPLACEME |
| 1903 | + pr-url: https://github.com/nodejs/node/pull/58849 |
| 1904 | + description: Message may now be a `printf`-like format string or function. |
1847 | 1905 | - version: v10.0.0 |
1848 | 1906 | pr-url: https://github.com/nodejs/node/pull/17003 |
1849 | 1907 | description: Used comparison changed from Strict Equality to `Object.is()`. |
1850 | 1908 | --> |
1851 | 1909 |
|
1852 | 1910 | * `actual` {any} |
1853 | 1911 | * `expected` {any} |
1854 | | -* `message` {string|Error} |
| 1912 | +* `message` {string|Error|Function} Postfix `printf`-like arguments in case |
| 1913 | + it's used as format string. |
| 1914 | + If message is a function, it is called in case of a comparison failure. The |
| 1915 | + function receives the `actual` and `expected` arguments and has to return a |
| 1916 | + string that is going to be used as error message. |
| 1917 | + `printf`-like format strings and functions are beneficial for performance |
| 1918 | + reasons in case arguments are passed through. In addition, it allows nice |
| 1919 | + formatting with ease. |
1855 | 1920 |
|
1856 | 1921 | Tests strict equality between the `actual` and `expected` parameters as |
1857 | 1922 | determined by [`Object.is()`][]. |
@@ -1880,8 +1945,17 @@ const oranges = 2; |
1880 | 1945 | assert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`); |
1881 | 1946 | // AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2 |
1882 | 1947 |
|
| 1948 | +assert.strictEqual(apples, oranges, 'apples %s !== oranges %s', apples, oranges); |
| 1949 | +// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2 |
| 1950 | + |
1883 | 1951 | assert.strictEqual(1, '1', new TypeError('Inputs are not identical')); |
1884 | 1952 | // TypeError: Inputs are not identical |
| 1953 | + |
| 1954 | +assert.strictEqual(apples, oranges, (actual, expected) => { |
| 1955 | + // Do 'heavy' computations |
| 1956 | + return `I expected ${expected} but I got ${actual}`; |
| 1957 | +}); |
| 1958 | +// AssertionError [ERR_ASSERTION]: I expected oranges but I got apples |
1885 | 1959 | ``` |
1886 | 1960 |
|
1887 | 1961 | ```cjs |
@@ -1908,8 +1982,17 @@ const oranges = 2; |
1908 | 1982 | assert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`); |
1909 | 1983 | // AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2 |
1910 | 1984 |
|
| 1985 | +assert.strictEqual(apples, oranges, 'apples %s !== oranges %s', apples, oranges); |
| 1986 | +// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2 |
| 1987 | + |
1911 | 1988 | assert.strictEqual(1, '1', new TypeError('Inputs are not identical')); |
1912 | 1989 | // TypeError: Inputs are not identical |
| 1990 | + |
| 1991 | +assert.strictEqual(apples, oranges, (actual, expected) => { |
| 1992 | + // Do 'heavy' computations |
| 1993 | + return `I expected ${expected} but I got ${actual}`; |
| 1994 | +}); |
| 1995 | +// AssertionError [ERR_ASSERTION]: I expected oranges but I got apples |
1913 | 1996 | ``` |
1914 | 1997 |
|
1915 | 1998 | If the values are not strictly equal, an [`AssertionError`][] is thrown with a |
@@ -2295,7 +2378,7 @@ changes: |
2295 | 2378 |
|
2296 | 2379 | * `actual` {any} |
2297 | 2380 | * `expected` {any} |
2298 | | -* `message` {string|Error} |
| 2381 | +* `message` {string|Error|Function} |
2299 | 2382 |
|
2300 | 2383 | Tests for partial deep equality between the `actual` and `expected` parameters. |
2301 | 2384 | "Deep" equality means that the enumerable "own" properties of child objects |
@@ -2460,5 +2543,6 @@ assert.partialDeepStrictEqual( |
2460 | 2543 | [`assert.strictEqual()`]: #assertstrictequalactual-expected-message |
2461 | 2544 | [`assert.throws()`]: #assertthrowsfn-error-message |
2462 | 2545 | [`getColorDepth()`]: tty.md#writestreamgetcolordepthenv |
| 2546 | +[`util.format()`]: util.md#utilformatformat-args |
2463 | 2547 | [enumerable "own" properties]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties |
2464 | 2548 | [prototype-spec]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots |