From 60e836427ee6549496269a7fd1ab44d8fd6d5e43 Mon Sep 17 00:00:00 2001 From: Jacob Hummer Date: Wed, 1 Nov 2023 12:54:05 -0500 Subject: [PATCH] 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 Reviewed-By: Matteo Collina --- lib/internal/console/constructor.js | 7 ++++++- test/message/console_assert.js | 5 +++++ test/message/console_assert.out | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 test/message/console_assert.js create mode 100644 test/message/console_assert.out diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index 44fa0f32d76ff3..0a5849d524f782 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -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); } diff --git a/test/message/console_assert.js b/test/message/console_assert.js new file mode 100644 index 00000000000000..14976312820d40 --- /dev/null +++ b/test/message/console_assert.js @@ -0,0 +1,5 @@ +'use strict'; + +require('../common'); + +console.assert(false, Symbol('hello')); diff --git a/test/message/console_assert.out b/test/message/console_assert.out new file mode 100644 index 00000000000000..259d18fa5cfc2f --- /dev/null +++ b/test/message/console_assert.out @@ -0,0 +1 @@ +Assertion failed* Symbol(hello)