console: treat non-strings as separate argument in console.assert()

fixes #49680

PR-URL: https://github.com/nodejs/node/pull/49722
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Jacob Hummer 2023-11-01 12:54:05 -05:00 committed by GitHub
parent 30950864d3
commit 60e836427e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 1 deletions

View File

@ -430,9 +430,14 @@ const consoleMethods = {
this.error(err.stack);
},
// Defined by: https://console.spec.whatwg.org/#assert
assert(expression, ...args) {
if (!expression) {
args[0] = `Assertion failed${args.length === 0 ? '' : `: ${args[0]}`}`;
if (args.length && typeof args[0] === 'string') {
args[0] = `Assertion failed: ${args[0]}`;
} else {
ArrayPrototypeUnshift(args, 'Assertion failed');
}
// The arguments will be formatted in warn() again
ReflectApply(this.warn, this, args);
}

View File

@ -0,0 +1,5 @@
'use strict';
require('../common');
console.assert(false, Symbol('hello'));

View File

@ -0,0 +1 @@
Assertion failed* Symbol(hello)