From 15d880bcb62c628f1e7c3cc7baf659a63b312c7c Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 16 Dec 2017 01:40:28 -0200 Subject: [PATCH] console: make .assert standard compliant The standard does not throw and has no stack trace. See https://console.spec.whatwg.org/#assert PR-URL: https://github.com/nodejs/node/pull/17706 Reviewed-By: James M Snell Reviewed-By: Anatoli Papirovski Reviewed-By: Matteo Collina --- doc/api/console.md | 70 ++++++++--------------------------- lib/console.js | 3 +- test/parallel/test-console.js | 21 +++++------ 3 files changed, 28 insertions(+), 66 deletions(-) diff --git a/doc/api/console.md b/doc/api/console.md index 84b7e2c11dac8e..76f6c6ac4623a7 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} +* `value` {any} The value tested for being truthy. +* `...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, -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 +console.assert(false, 'Whoops %s work', 'didn\'t'); +// Assertion failed: 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; -``` - -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()