From f70f516dd8235d0015a6151f0437585451e7c3fa Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 16 Dec 2017 01:40:28 -0200 Subject: [PATCH 1/4] console: make .assert standard compliant The standard has no stack trace. See https://console.spec.whatwg.org/#assert --- doc/api/console.md | 65 ++++++++--------------------------- lib/console.js | 3 +- test/parallel/test-console.js | 21 ++++++----- 3 files changed, 26 insertions(+), 63 deletions(-) diff --git a/doc/api/console.md b/doc/api/console.md index 5722f362841484..bec5644d05d8c9 100644 --- a/doc/api/console.md +++ b/doc/api/console.md @@ -103,70 +103,33 @@ The global `console` is a special `Console` whose output is sent to new Console(process.stdout, process.stderr); ``` -### console.assert(value[, message][, ...args]) +### console.assert(value[, ...message]) * `value` {any} * `message` {any} -* `...args` {any} A simple assertion test that verifies whether `value` is truthy. If it is not, -an `AssertionError` is thrown. If provided, the error `message` is formatted -using [`util.format()`][] and used as the error message. +`Assertion failed` is logged. If provided, the error `message` is formatted +using [`util.format()`][] by passing along all message arguments. The output is +used as the error message. ```js console.assert(true, 'does nothing'); // OK -console.assert(false, 'Whoops %s', 'didn\'t work'); -// AssertionError: Whoops didn't work -``` - -*Note*: The `console.assert()` method is implemented differently in Node.js -than the `console.assert()` method [available in browsers][web-api-assert]. - -Specifically, in browsers, calling `console.assert()` with a falsy -assertion will cause the `message` to be printed to the console without -interrupting execution of subsequent code. In Node.js, however, a falsy -assertion will cause an `AssertionError` to be thrown. - -Functionality approximating that implemented by browsers can be implemented -by extending Node.js' `console` and overriding the `console.assert()` method. - -In the following example, a simple module is created that extends and overrides -the default behavior of `console` in Node.js. - - -```js -'use strict'; - -// Creates a simple extension of console with a -// new impl for assert without monkey-patching. -const myConsole = Object.create(console, { - assert: { - value: function assert(assertion, message, ...args) { - try { - console.assert(assertion, message, ...args); - } catch (err) { - console.error(err.stack); - } - }, - configurable: true, - enumerable: true, - writable: true, - }, -}); - -module.exports = myConsole; +console.assert(false, 'Whoops %s work', 'didn\'t'); +// Assertion failed: Whoops didn't work ``` -This can then be used as a direct replacement for the built in console: - -```js -const console = require('./myConsole'); -console.assert(false, 'this message will print, but no error thrown'); -console.log('this will also print'); -``` +*Note*: Calling `console.assert()` with a falsy assertion will only cause the +`message` to be printed to the console without interrupting execution of +subsequent code. ### console.clear() -* `value` {any} -* `message` {any} +* `value` {any} The value tested for being truthy. +* `...message` {any} All arguments besides the first are used as error message. A simple assertion test that verifies whether `value` is truthy. If it is not, `Assertion failed` is logged. If provided, the error `message` is formatted From a87fd312d3d5bb853e2e9c600570993bce1b189a Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 21 Dec 2017 09:59:23 -0300 Subject: [PATCH 3/4] fixup --- doc/api/console.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/console.md b/doc/api/console.md index 4f8c226ccea0fe..5f0c7d1c1c2343 100644 --- a/doc/api/console.md +++ b/doc/api/console.md @@ -113,7 +113,7 @@ changes: [web-api-assert][]) and does not throw anymore. --> * `value` {any} The value tested for being truthy. -* `...message` {any} All arguments besides the first are used as error message. +* `...message` {any} All arguments besides `value` are used as error message. A simple assertion test that verifies whether `value` is truthy. If it is not, `Assertion failed` is logged. If provided, the error `message` is formatted From af54a1261679abe545359068d6435b466fabd96a Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 21 Dec 2017 10:36:39 -0300 Subject: [PATCH 4/4] fixup md linter issue --- doc/api/console.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/doc/api/console.md b/doc/api/console.md index 5f0c7d1c1c2343..f8ba8214497a45 100644 --- a/doc/api/console.md +++ b/doc/api/console.md @@ -109,8 +109,8 @@ added: v0.1.101 changes: - version: REPLACEME pr-url: https://github.com/nodejs/node/pull/REPLACEME - description: The implementation is now spec compliant (see - [web-api-assert][]) and does not throw anymore. + description: The implementation is now spec compliant and does not throw + anymore. --> * `value` {any} The value tested for being truthy. * `...message` {any} All arguments besides `value` are used as error message. @@ -499,4 +499,3 @@ This method does not display anything unless used in the inspector. The [customizing `util.inspect()` colors]: util.html#util_customizing_util_inspect_colors [inspector]: debugger.html [note on process I/O]: process.html#process_a_note_on_process_i_o -[web-api-assert]: https://developer.mozilla.org/en-US/docs/Web/API/console/assert