Skip to content

Commit

Permalink
console: treat non-strings as separate argument in console.assert()
Browse files Browse the repository at this point in the history
fixes nodejs#49680

PR-URL: nodejs#49722
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
jcbhmr authored and anonrig committed Nov 9, 2023
1 parent 4172dd3 commit 49ae441
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 5 additions & 0 deletions test/message/console_assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

require('../common');

console.assert(false, Symbol('hello'));
1 change: 1 addition & 0 deletions test/message/console_assert.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Assertion failed* Symbol(hello)

0 comments on commit 49ae441

Please sign in to comment.