Skip to content

Commit e65a6e8

Browse files
committedJan 24, 2018
assert: stricter ifError
This makes `assert.ifError` stricter by only accepting `null` and `undefined` from now on. Before any truthy value was accepted. PR-URL: #18247 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8e6e1c9 commit e65a6e8

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed
 

‎doc/api/assert.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -474,21 +474,23 @@ changes:
474474
pr-url: https://github.com/nodejs/node/pull/18247
475475
description: Instead of throwing the original error it is now wrapped into
476476
a AssertionError that contains the full stack trace.
477+
- version: REPLACEME
478+
pr-url: https://github.com/nodejs/node/pull/18247
479+
description: Value may now only be `undefined` or `null`. Before any truthy
480+
input was accepted.
477481
-->
478482
* `value` {any}
479483

480-
Throws `value` if `value` is truthy. This is useful when testing the `error`
481-
argument in callbacks.
484+
Throws `value` if `value` is not `undefined` or `null`. This is useful when
485+
testing the `error` argument in callbacks.
482486

483487
```js
484488
const assert = require('assert').strict;
485489

486490
assert.ifError(null);
487491
// OK
488492
assert.ifError(0);
489-
// OK
490-
assert.ifError(1);
491-
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 1
493+
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0
492494
assert.ifError('error');
493495
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'
494496
assert.ifError(new Error());

‎lib/assert.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ assert.doesNotThrow = function doesNotThrow(block, error, message) {
453453
};
454454

455455
assert.ifError = function ifError(err) {
456-
if (err) {
456+
if (err !== null && err !== undefined) {
457457
let message = 'ifError got unwanted exception: ';
458458
if (typeof err === 'object' && typeof err.message === 'string') {
459459
if (err.message.length === 0 && err.constructor) {

‎test/parallel/test-assert-if-error.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,16 @@ assert.throws(
6060
}
6161
);
6262

63+
assert.throws(
64+
() => { assert.ifError(false); },
65+
{
66+
message: 'ifError got unwanted exception: false'
67+
}
68+
);
69+
6370
assert.doesNotThrow(() => { assert.ifError(null); });
6471
assert.doesNotThrow(() => { assert.ifError(); });
6572
assert.doesNotThrow(() => { assert.ifError(undefined); });
66-
assert.doesNotThrow(() => { assert.ifError(false); });
6773

6874
// https://github.com/nodejs/node-v0.x-archive/issues/2893
6975
{

‎test/sequential/test-inspector-port-zero.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ function test(arg, port = '') {
1616
let stderr = '';
1717
proc.stdout.on('data', (data) => stdout += data);
1818
proc.stderr.on('data', (data) => stderr += data);
19-
proc.stdout.on('close', assert.ifError);
20-
proc.stderr.on('close', assert.ifError);
19+
proc.stdout.on('close', (hadErr) => assert(!hadErr));
20+
proc.stderr.on('close', (hadErr) => assert(!hadErr));
2121
proc.stderr.on('data', () => {
2222
if (!stderr.includes('\n')) return;
2323
assert(/Debugger listening on (.+)/.test(stderr));

0 commit comments

Comments
 (0)
Please sign in to comment.