Skip to content

Commit 15d880b

Browse files
committedDec 26, 2017
console: make .assert standard compliant
The standard does not throw and has no stack trace. See https://console.spec.whatwg.org/#assert PR-URL: #17706 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 9ca4ab1 commit 15d880b

File tree

3 files changed

+28
-66
lines changed

3 files changed

+28
-66
lines changed
 

‎doc/api/console.md

+16-54
Original file line numberDiff line numberDiff line change
@@ -103,70 +103,33 @@ The global `console` is a special `Console` whose output is sent to
103103
new Console(process.stdout, process.stderr);
104104
```
105105

106-
### console.assert(value[, message][, ...args])
106+
### console.assert(value[, ...message])
107107
<!-- YAML
108108
added: v0.1.101
109+
changes:
110+
- version: REPLACEME
111+
pr-url: https://github.com/nodejs/node/pull/REPLACEME
112+
description: The implementation is now spec compliant and does not throw
113+
anymore.
109114
-->
110-
* `value` {any}
111-
* `message` {any}
112-
* `...args` {any}
115+
* `value` {any} The value tested for being truthy.
116+
* `...message` {any} All arguments besides `value` are used as error message.
113117

114118
A simple assertion test that verifies whether `value` is truthy. If it is not,
115-
an `AssertionError` is thrown. If provided, the error `message` is formatted
116-
using [`util.format()`][] and used as the error message.
119+
`Assertion failed` is logged. If provided, the error `message` is formatted
120+
using [`util.format()`][] by passing along all message arguments. The output is
121+
used as the error message.
117122

118123
```js
119124
console.assert(true, 'does nothing');
120125
// OK
121-
console.assert(false, 'Whoops %s', 'didn\'t work');
122-
// AssertionError: Whoops didn't work
126+
console.assert(false, 'Whoops %s work', 'didn\'t');
127+
// Assertion failed: Whoops didn't work
123128
```
124129

125-
*Note*: The `console.assert()` method is implemented differently in Node.js
126-
than the `console.assert()` method [available in browsers][web-api-assert].
127-
128-
Specifically, in browsers, calling `console.assert()` with a falsy
129-
assertion will cause the `message` to be printed to the console without
130-
interrupting execution of subsequent code. In Node.js, however, a falsy
131-
assertion will cause an `AssertionError` to be thrown.
132-
133-
Functionality approximating that implemented by browsers can be implemented
134-
by extending Node.js' `console` and overriding the `console.assert()` method.
135-
136-
In the following example, a simple module is created that extends and overrides
137-
the default behavior of `console` in Node.js.
138-
139-
<!-- eslint-disable func-name-matching -->
140-
```js
141-
'use strict';
142-
143-
// Creates a simple extension of console with a
144-
// new impl for assert without monkey-patching.
145-
const myConsole = Object.create(console, {
146-
assert: {
147-
value: function assert(assertion, message, ...args) {
148-
try {
149-
console.assert(assertion, message, ...args);
150-
} catch (err) {
151-
console.error(err.stack);
152-
}
153-
},
154-
configurable: true,
155-
enumerable: true,
156-
writable: true,
157-
},
158-
});
159-
160-
module.exports = myConsole;
161-
```
162-
163-
This can then be used as a direct replacement for the built in console:
164-
165-
```js
166-
const console = require('./myConsole');
167-
console.assert(false, 'this message will print, but no error thrown');
168-
console.log('this will also print');
169-
```
130+
*Note*: Calling `console.assert()` with a falsy assertion will only cause the
131+
`message` to be printed to the console without interrupting execution of
132+
subsequent code.
170133

171134
### console.clear()
172135
<!-- YAML
@@ -531,4 +494,3 @@ This method does not display anything unless used in the inspector. The
531494
[customizing `util.inspect()` colors]: util.html#util_customizing_util_inspect_colors
532495
[inspector]: debugger.html
533496
[note on process I/O]: process.html#process_a_note_on_process_i_o
534-
[web-api-assert]: https://developer.mozilla.org/en-US/docs/Web/API/console/assert

‎lib/console.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ Console.prototype.trace = function trace(...args) {
194194

195195
Console.prototype.assert = function assert(expression, ...args) {
196196
if (!expression) {
197-
require('assert').ok(false, util.format.apply(null, args));
197+
args[0] = `Assertion failed${args.length === 0 ? '' : `: ${args[0]}`}`;
198+
this.warn(util.format.apply(null, args));
198199
}
199200
};
200201

‎test/parallel/test-console.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ console.timeEnd();
140140
console.time(NaN);
141141
console.timeEnd(NaN);
142142

143+
assert.doesNotThrow(() => {
144+
console.assert(false, '%s should', 'console.assert', 'not throw');
145+
assert.strictEqual(errStrings[errStrings.length - 1],
146+
'Assertion failed: console.assert should not throw\n');
147+
});
148+
149+
assert.doesNotThrow(() => {
150+
console.assert(true, 'this should not throw');
151+
});
152+
143153
assert.strictEqual(strings.length, process.stdout.writeTimes);
144154
assert.strictEqual(errStrings.length, process.stderr.writeTimes);
145155
common.restoreStdout();
@@ -200,17 +210,6 @@ assert.ok(/^NaN: \d+\.\d{3}ms$/.test(strings.shift().trim()));
200210
assert.strictEqual(errStrings.shift().split('\n').shift(),
201211
'Trace: This is a {"formatted":"trace"} 10 foo');
202212

203-
common.expectsError(() => {
204-
console.assert(false, 'should throw');
205-
}, {
206-
code: 'ERR_ASSERTION',
207-
message: /^should throw$/
208-
});
209-
210-
assert.doesNotThrow(() => {
211-
console.assert(true, 'this should not throw');
212-
});
213-
214213
// hijack stderr to catch `process.emitWarning` which is using
215214
// `process.nextTick`
216215
common.hijackStderr(common.mustCall(function(data) {

0 commit comments

Comments
 (0)
Please sign in to comment.