summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2018-01-23 14:07:18 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2018-01-24 13:16:09 +0100
commite65a6e81ef5e8c0afae4ffec852b662732114adb (patch)
treef07404a4983170a8cce56a481300a96ac3536d59
parent8e6e1c9dcc83843aa17225b14ef91b92f337aebb (diff)
downloadandroid-node-v8-e65a6e81ef5e8c0afae4ffec852b662732114adb.tar.gz
android-node-v8-e65a6e81ef5e8c0afae4ffec852b662732114adb.tar.bz2
android-node-v8-e65a6e81ef5e8c0afae4ffec852b662732114adb.zip
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: https://github.com/nodejs/node/pull/18247 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r--doc/api/assert.md12
-rw-r--r--lib/assert.js2
-rw-r--r--test/parallel/test-assert-if-error.js8
-rw-r--r--test/sequential/test-inspector-port-zero.js4
4 files changed, 17 insertions, 9 deletions
diff --git a/doc/api/assert.md b/doc/api/assert.md
index 80b3019ad8..04f312aa66 100644
--- a/doc/api/assert.md
+++ b/doc/api/assert.md
@@ -474,11 +474,15 @@ changes:
pr-url: https://github.com/nodejs/node/pull/18247
description: Instead of throwing the original error it is now wrapped into
a AssertionError that contains the full stack trace.
+ - version: REPLACEME
+ pr-url: https://github.com/nodejs/node/pull/18247
+ description: Value may now only be `undefined` or `null`. Before any truthy
+ input was accepted.
-->
* `value` {any}
-Throws `value` if `value` is truthy. This is useful when testing the `error`
-argument in callbacks.
+Throws `value` if `value` is not `undefined` or `null`. This is useful when
+testing the `error` argument in callbacks.
```js
const assert = require('assert').strict;
@@ -486,9 +490,7 @@ const assert = require('assert').strict;
assert.ifError(null);
// OK
assert.ifError(0);
-// OK
-assert.ifError(1);
-// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 1
+// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 0
assert.ifError('error');
// AssertionError [ERR_ASSERTION]: ifError got unwanted exception: 'error'
assert.ifError(new Error());
diff --git a/lib/assert.js b/lib/assert.js
index 20f9d0e477..e25247dfed 100644
--- a/lib/assert.js
+++ b/lib/assert.js
@@ -453,7 +453,7 @@ assert.doesNotThrow = function doesNotThrow(block, error, message) {
};
assert.ifError = function ifError(err) {
- if (err) {
+ if (err !== null && err !== undefined) {
let message = 'ifError got unwanted exception: ';
if (typeof err === 'object' && typeof err.message === 'string') {
if (err.message.length === 0 && err.constructor) {
diff --git a/test/parallel/test-assert-if-error.js b/test/parallel/test-assert-if-error.js
index bcee765f9a..3b070aac08 100644
--- a/test/parallel/test-assert-if-error.js
+++ b/test/parallel/test-assert-if-error.js
@@ -60,10 +60,16 @@ assert.throws(
}
);
+assert.throws(
+ () => { assert.ifError(false); },
+ {
+ message: 'ifError got unwanted exception: false'
+ }
+);
+
assert.doesNotThrow(() => { assert.ifError(null); });
assert.doesNotThrow(() => { assert.ifError(); });
assert.doesNotThrow(() => { assert.ifError(undefined); });
-assert.doesNotThrow(() => { assert.ifError(false); });
// https://github.com/nodejs/node-v0.x-archive/issues/2893
{
diff --git a/test/sequential/test-inspector-port-zero.js b/test/sequential/test-inspector-port-zero.js
index b7eb13ba13..59027b5e30 100644
--- a/test/sequential/test-inspector-port-zero.js
+++ b/test/sequential/test-inspector-port-zero.js
@@ -16,8 +16,8 @@ function test(arg, port = '') {
let stderr = '';
proc.stdout.on('data', (data) => stdout += data);
proc.stderr.on('data', (data) => stderr += data);
- proc.stdout.on('close', assert.ifError);
- proc.stderr.on('close', assert.ifError);
+ proc.stdout.on('close', (hadErr) => assert(!hadErr));
+ proc.stderr.on('close', (hadErr) => assert(!hadErr));
proc.stderr.on('data', () => {
if (!stderr.includes('\n')) return;
assert(/Debugger listening on (.+)/.test(stderr));