From 591a6e392e92dd10f6539bb1b606acee47dd05cf Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 26 Jun 2025 18:49:38 +0200 Subject: [PATCH 1/5] assert: allow printf-style messages as assertion error Also add functions as allowed message input. This allows to have leavy message computation to become cheaper. --- doc/api/assert.md | 86 +++++++++++++++--- lib/assert.js | 113 ++++++++++++++++-------- test/parallel/test-assert.js | 165 +++++++++++++++++++++++++++++++++++ 3 files changed, 315 insertions(+), 49 deletions(-) diff --git a/doc/api/assert.md b/doc/api/assert.md index e52b9b57c2b43a..4291b5386097aa 100644 --- a/doc/api/assert.md +++ b/doc/api/assert.md @@ -305,10 +305,14 @@ destructuring and call methods directly on the instance. * `value` {any} The input that is checked for being truthy. -* `message` {string|Error} +* `message` {string|Error|Function} An alias of [`assert.ok()`][]. @@ -324,6 +328,9 @@ changes: - version: REPLACEME pr-url: https://github.com/nodejs/node/pull/57627 description: Invalid dates are now considered equal. + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/58849 + description: Message may now be a `printf`-like format string or function. - version: v24.0.0 pr-url: https://github.com/nodejs/node/pull/57622 description: Recursion now stops when either side encounters a circular @@ -375,7 +382,7 @@ changes: * `actual` {any} * `expected` {any} -* `message` {string|Error} +* `message` {string|Error|Function} **Strict assertion mode** @@ -524,6 +531,9 @@ changes: - version: REPLACEME pr-url: https://github.com/nodejs/node/pull/57627 description: Invalid dates are now considered equal. + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/58849 + description: Message may now be a `printf`-like format string or function. - version: v24.0.0 pr-url: https://github.com/nodejs/node/pull/57622 description: Recursion now stops when either side encounters a circular @@ -567,7 +577,7 @@ changes: * `actual` {any} * `expected` {any} -* `message` {string|Error} +* `message` {string|Error|Function} Tests for deep equality between the `actual` and `expected` parameters. "Deep" equality means that the enumerable "own" properties of child objects @@ -829,6 +839,9 @@ added: - v13.6.0 - v12.16.0 changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/58849 + description: Message may now be a `printf`-like format string or function. - version: v16.0.0 pr-url: https://github.com/nodejs/node/pull/38111 description: This API is no longer experimental. @@ -836,7 +849,7 @@ changes: * `string` {string} * `regexp` {RegExp} -* `message` {string|Error} +* `message` {string|Error|Function} Expects the `string` input not to match the regular expression. @@ -1069,6 +1082,9 @@ assert.doesNotThrow( * `value` {any} -* `message` {string|Error} +* `message` {string|Error|Function} Tests if `value` is truthy. It is equivalent to `assert.equal(!!value, true, message)`. @@ -1844,6 +1878,9 @@ argument gets considered.