@@ -103,70 +103,33 @@ The global `console` is a special `Console` whose output is sent to
103
103
new Console (process .stdout , process .stderr );
104
104
```
105
105
106
- ### console.assert(value[ , message ] [ , ...args ] )
106
+ ### console.assert(value[ , ...message ] )
107
107
<!-- YAML
108
108
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.
109
114
-->
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.
113
117
114
118
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.
117
122
118
123
``` js
119
124
console .assert (true , ' does nothing' );
120
125
// 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
123
128
```
124
129
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.
170
133
171
134
### console.clear()
172
135
<!-- YAML
@@ -531,4 +494,3 @@ This method does not display anything unless used in the inspector. The
531
494
[ customizing `util.inspect()` colors ] : util.html#util_customizing_util_inspect_colors
532
495
[ inspector ] : debugger.html
533
496
[ 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
0 commit comments