Skip to content

Commit ae6214e

Browse files
committed
fixup! additional types, docs, and tests
1 parent b4db0b2 commit ae6214e

File tree

3 files changed

+91
-26
lines changed

3 files changed

+91
-26
lines changed

doc/api/assert.md

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,14 @@ destructuring and call methods directly on the instance.
268268

269269
<!-- YAML
270270
added: v0.5.9
271+
changes:
272+
- version: REPLACEME
273+
pr-url: https://github.com/nodejs/node/pull/58849
274+
description: Message may now be a `printf`-like format string or function.
271275
-->
272276

273277
* `value` {any} The input that is checked for being truthy.
274-
* `message` {string|Error}
278+
* `message` {string|Error|Function}
275279

276280
An alias of [`assert.ok()`][].
277281

@@ -287,6 +291,9 @@ changes:
287291
- version: REPLACEME
288292
pr-url: https://github.com/nodejs/node/pull/57627
289293
description: Invalid dates are now considered equal.
294+
- version: REPLACEME
295+
pr-url: https://github.com/nodejs/node/pull/58849
296+
description: Message may now be a `printf`-like format string or function.
290297
- version: v24.0.0
291298
pr-url: https://github.com/nodejs/node/pull/57622
292299
description: Recursion now stops when either side encounters a circular
@@ -338,7 +345,7 @@ changes:
338345

339346
* `actual` {any}
340347
* `expected` {any}
341-
* `message` {string|Error}
348+
* `message` {string|Error|Function}
342349

343350
**Strict assertion mode**
344351

@@ -487,6 +494,9 @@ changes:
487494
- version: REPLACEME
488495
pr-url: https://github.com/nodejs/node/pull/57627
489496
description: Invalid dates are now considered equal.
497+
- version: REPLACEME
498+
pr-url: https://github.com/nodejs/node/pull/58849
499+
description: Message may now be a `printf`-like format string or function.
490500
- version: v24.0.0
491501
pr-url: https://github.com/nodejs/node/pull/57622
492502
description: Recursion now stops when either side encounters a circular
@@ -530,7 +540,7 @@ changes:
530540

531541
* `actual` {any}
532542
* `expected` {any}
533-
* `message` {string|Error}
543+
* `message` {string|Error|Function}
534544

535545
Tests for deep equality between the `actual` and `expected` parameters.
536546
"Deep" equality means that the enumerable "own" properties of child objects
@@ -792,14 +802,17 @@ added:
792802
- v13.6.0
793803
- v12.16.0
794804
changes:
805+
- version: REPLACEME
806+
pr-url: https://github.com/nodejs/node/pull/58849
807+
description: Message may now be a `printf`-like format string or function.
795808
- version: v16.0.0
796809
pr-url: https://github.com/nodejs/node/pull/38111
797810
description: This API is no longer experimental.
798811
-->
799812

800813
* `string` {string}
801814
* `regexp` {RegExp}
802-
* `message` {string|Error}
815+
* `message` {string|Error|Function}
803816

804817
Expects the `string` input not to match the regular expression.
805818

@@ -1032,6 +1045,9 @@ assert.doesNotThrow(
10321045
<!-- YAML
10331046
added: v0.1.21
10341047
changes:
1048+
- version: REPLACEME
1049+
pr-url: https://github.com/nodejs/node/pull/58849
1050+
description: Message may now be a `printf`-like format string or function.
10351051
- version:
10361052
- v16.0.0
10371053
- v14.18.0
@@ -1046,7 +1062,7 @@ changes:
10461062

10471063
* `actual` {any}
10481064
* `expected` {any}
1049-
* `message` {string|Error}
1065+
* `message` {string|Error|Function}
10501066

10511067
**Strict assertion mode**
10521068

@@ -1217,14 +1233,17 @@ added:
12171233
- v13.6.0
12181234
- v12.16.0
12191235
changes:
1236+
- version: REPLACEME
1237+
pr-url: https://github.com/nodejs/node/pull/58849
1238+
description: Message may now be a `printf`-like format string or function.
12201239
- version: v16.0.0
12211240
pr-url: https://github.com/nodejs/node/pull/38111
12221241
description: This API is no longer experimental.
12231242
-->
12241243

12251244
* `string` {string}
12261245
* `regexp` {RegExp}
1227-
* `message` {string|Error}
1246+
* `message` {string|Error|Function}
12281247

12291248
Expects the `string` input to match the regular expression.
12301249

@@ -1266,6 +1285,9 @@ instance of {Error} then it will be thrown instead of the
12661285
<!-- YAML
12671286
added: v0.1.21
12681287
changes:
1288+
- version: REPLACEME
1289+
pr-url: https://github.com/nodejs/node/pull/58849
1290+
description: Message may now be a `printf`-like format string or function.
12691291
- version:
12701292
- v16.0.0
12711293
- v14.18.0
@@ -1301,7 +1323,7 @@ changes:
13011323

13021324
* `actual` {any}
13031325
* `expected` {any}
1304-
* `message` {string|Error}
1326+
* `message` {string|Error|Function}
13051327

13061328
**Strict assertion mode**
13071329

@@ -1390,6 +1412,9 @@ instead of the `AssertionError`.
13901412
<!-- YAML
13911413
added: v1.2.0
13921414
changes:
1415+
- version: REPLACEME
1416+
pr-url: https://github.com/nodejs/node/pull/58849
1417+
description: Message may now be a `printf`-like format string or function.
13931418
- version: v9.0.0
13941419
pr-url: https://github.com/nodejs/node/pull/15398
13951420
description: The `-0` and `+0` are not considered equal anymore.
@@ -1421,7 +1446,7 @@ changes:
14211446

14221447
* `actual` {any}
14231448
* `expected` {any}
1424-
* `message` {string|Error}
1449+
* `message` {string|Error|Function}
14251450

14261451
Tests for deep strict inequality. Opposite of [`assert.deepStrictEqual()`][].
14271452

@@ -1450,6 +1475,9 @@ instead of the [`AssertionError`][].
14501475
<!-- YAML
14511476
added: v0.1.21
14521477
changes:
1478+
- version: REPLACEME
1479+
pr-url: https://github.com/nodejs/node/pull/58849
1480+
description: Message may now be a `printf`-like format string or function.
14531481
- version:
14541482
- v16.0.0
14551483
- v14.18.0
@@ -1464,7 +1492,7 @@ changes:
14641492

14651493
* `actual` {any}
14661494
* `expected` {any}
1467-
* `message` {string|Error}
1495+
* `message` {string|Error|Function}
14681496

14691497
**Strict assertion mode**
14701498

@@ -1514,14 +1542,17 @@ parameter is an instance of {Error} then it will be thrown instead of the
15141542
<!-- YAML
15151543
added: v0.1.21
15161544
changes:
1545+
- version: REPLACEME
1546+
pr-url: https://github.com/nodejs/node/pull/58849
1547+
description: Message may now be a `printf`-like format string or function.
15171548
- version: v10.0.0
15181549
pr-url: https://github.com/nodejs/node/pull/17003
15191550
description: Used comparison changed from Strict Equality to `Object.is()`.
15201551
-->
15211552

15221553
* `actual` {any}
15231554
* `expected` {any}
1524-
* `message` {string|Error}
1555+
* `message` {string|Error|Function}
15251556

15261557
Tests strict inequality between the `actual` and `expected` parameters as
15271558
determined by [`Object.is()`][].
@@ -1567,14 +1598,17 @@ instead of the `AssertionError`.
15671598
<!-- YAML
15681599
added: v0.1.21
15691600
changes:
1601+
- version: REPLACEME
1602+
pr-url: https://github.com/nodejs/node/pull/58849
1603+
description: Message may now be a `printf`-like format string or function.
15701604
- version: v10.0.0
15711605
pr-url: https://github.com/nodejs/node/pull/18319
15721606
description: The `assert.ok()` (no arguments) will now use a predefined
15731607
error message.
15741608
-->
15751609

15761610
* `value` {any}
1577-
* `message` {string|Error}
1611+
* `message` {string|Error|Function}
15781612

15791613
Tests if `value` is truthy. It is equivalent to
15801614
`assert.equal(!!value, true, message)`.
@@ -1808,7 +1842,7 @@ argument gets considered.
18081842
added: v0.1.21
18091843
changes:
18101844
- version: REPLACEME
1811-
pr-url: https://github.com/nodejs/node/pull/XXXXX
1845+
pr-url: https://github.com/nodejs/node/pull/58849
18121846
description: Message may now be a `printf`-like format string or function.
18131847
- version: v10.0.0
18141848
pr-url: https://github.com/nodejs/node/pull/17003
@@ -1859,11 +1893,11 @@ assert.strictEqual(apples, oranges, 'apples %s !== oranges %s', apples, oranges)
18591893
assert.strictEqual(1, '1', new TypeError('Inputs are not identical'));
18601894
// TypeError: Inputs are not identical
18611895

1862-
assert.strictEqual(apples, oranges, () => {
1896+
assert.strictEqual(apples, oranges, (actual, expected) => {
18631897
// Do 'heavy' computations
1864-
return 'My error string';
1898+
return `I expected ${expected} but I got ${actual}`;
18651899
});
1866-
// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2
1900+
// AssertionError [ERR_ASSERTION]: I expected oranges but I got apples
18671901
```
18681902

18691903
```cjs
@@ -1890,8 +1924,17 @@ const oranges = 2;
18901924
assert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`);
18911925
// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2
18921926

1927+
assert.strictEqual(apples, oranges, 'apples %s !== oranges %s', apples, oranges);
1928+
// AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2
1929+
18931930
assert.strictEqual(1, '1', new TypeError('Inputs are not identical'));
18941931
// TypeError: Inputs are not identical
1932+
1933+
assert.strictEqual(apples, oranges, (actual, expected) => {
1934+
// Do 'heavy' computations
1935+
return `I expected ${expected} but I got ${actual}`;
1936+
});
1937+
// AssertionError [ERR_ASSERTION]: I expected oranges but I got apples
18951938
```
18961939

18971940
If the values are not strictly equal, an [`AssertionError`][] is thrown with a

lib/assert.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ Assert.prototype.ok = function ok(...args) {
248248
* The equality assertion tests shallow, coercive equality with ==.
249249
* @param {any} actual
250250
* @param {any} expected
251-
* @param {string | Error} [message]
251+
* @param {string | Error | Function} [message]
252252
* @returns {void}
253253
*/
254254
Assert.prototype.equal = function equal(actual, expected, ...message) {
@@ -273,7 +273,7 @@ Assert.prototype.equal = function equal(actual, expected, ...message) {
273273
* equal with !=.
274274
* @param {any} actual
275275
* @param {any} expected
276-
* @param {string | Error} [message]
276+
* @param {string | Error | Function} [message]
277277
* @returns {void}
278278
*/
279279
Assert.prototype.notEqual = function notEqual(actual, expected, ...message) {
@@ -297,7 +297,7 @@ Assert.prototype.notEqual = function notEqual(actual, expected, ...message) {
297297
* The deep equivalence assertion tests a deep equality relation.
298298
* @param {any} actual
299299
* @param {any} expected
300-
* @param {string | Error} [message]
300+
* @param {string | Error | Function} [message]
301301
* @returns {void}
302302
*/
303303
Assert.prototype.deepEqual = function deepEqual(actual, expected, ...message) {
@@ -321,7 +321,7 @@ Assert.prototype.deepEqual = function deepEqual(actual, expected, ...message) {
321321
* The deep non-equivalence assertion tests for any deep inequality.
322322
* @param {any} actual
323323
* @param {any} expected
324-
* @param {string | Error} [message]
324+
* @param {string | Error | Function} [message]
325325
* @returns {void}
326326
*/
327327
Assert.prototype.notDeepEqual = function notDeepEqual(actual, expected, ...message) {
@@ -346,7 +346,7 @@ Assert.prototype.notDeepEqual = function notDeepEqual(actual, expected, ...messa
346346
* relation.
347347
* @param {any} actual
348348
* @param {any} expected
349-
* @param {string | Error} [message]
349+
* @param {string | Error | Function} [message]
350350
* @returns {void}
351351
*/
352352
Assert.prototype.deepStrictEqual = function deepStrictEqual(actual, expected, ...message) {
@@ -371,7 +371,7 @@ Assert.prototype.deepStrictEqual = function deepStrictEqual(actual, expected, ..
371371
* inequality.
372372
* @param {any} actual
373373
* @param {any} expected
374-
* @param {string | Error} [message]
374+
* @param {string | Error | Function} [message]
375375
* @returns {void}
376376
*/
377377
Assert.prototype.notDeepStrictEqual = notDeepStrictEqual;
@@ -396,7 +396,7 @@ function notDeepStrictEqual(actual, expected, ...message) {
396396
* The strict equivalence assertion tests a strict equality relation.
397397
* @param {any} actual
398398
* @param {any} expected
399-
* @param {string | Error} [message]
399+
* @param {string | Error | Function} [message]
400400
* @returns {void}
401401
*/
402402
Assert.prototype.strictEqual = function strictEqual(actual, expected, ...message) {
@@ -419,7 +419,7 @@ Assert.prototype.strictEqual = function strictEqual(actual, expected, ...message
419419
* The strict non-equivalence assertion tests for any strict inequality.
420420
* @param {any} actual
421421
* @param {any} expected
422-
* @param {string | Error} [message]
422+
* @param {string | Error | Function} [message]
423423
* @returns {void}
424424
*/
425425
Assert.prototype.notStrictEqual = function notStrictEqual(actual, expected, ...message) {
@@ -442,7 +442,7 @@ Assert.prototype.notStrictEqual = function notStrictEqual(actual, expected, ...m
442442
* The strict equivalence assertion test between two objects
443443
* @param {any} actual
444444
* @param {any} expected
445-
* @param {string | Error} [message]
445+
* @param {string | Error | Function} [message]
446446
* @returns {void}
447447
*/
448448
Assert.prototype.partialDeepStrictEqual = function partialDeepStrictEqual(
@@ -902,7 +902,7 @@ function internalMatch(string, regexp, message, fn) {
902902
* Expects the `string` input to match the regular expression.
903903
* @param {string} string
904904
* @param {RegExp} regexp
905-
* @param {string | Error} [message]
905+
* @param {string | Error | Function} [message]
906906
* @returns {void}
907907
*/
908908
Assert.prototype.match = function match(string, regexp, ...message) {
@@ -913,7 +913,7 @@ Assert.prototype.match = function match(string, regexp, ...message) {
913913
* Expects the `string` input not to match the regular expression.
914914
* @param {string} string
915915
* @param {RegExp} regexp
916-
* @param {string | Error} [message]
916+
* @param {string | Error | Function} [message]
917917
* @returns {void}
918918
*/
919919
Assert.prototype.doesNotMatch = function doesNotMatch(string, regexp, ...message) {

test/parallel/test-assert.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,5 +1738,27 @@ test('Ambiguous error messages fail', () => {
17381738
);
17391739
});
17401740

1741+
test('Functions as error message', () => {
1742+
function errorMessage(actual, expected) {
1743+
return `Nice message including ${actual} and ${expected}`;
1744+
}
1745+
1746+
assert.throws(
1747+
() => assert.equal('foo', 'bar', errorMessage),
1748+
{
1749+
code: 'ERR_ASSERTION',
1750+
message: /Nice message including foo and bar/
1751+
}
1752+
);
1753+
1754+
assert.throws(
1755+
() => assert.doesNotMatch('foo', /foo/, errorMessage),
1756+
{
1757+
code: 'ERR_ASSERTION',
1758+
message: /Nice message including foo and \/foo\//
1759+
}
1760+
);
1761+
});
1762+
17411763
/* eslint-enable no-restricted-syntax */
17421764
/* eslint-enable no-restricted-properties */

0 commit comments

Comments
 (0)