Skip to content

Commit

Permalink
test: remove assert.doesNotThrow()
Browse files Browse the repository at this point in the history
There is actually no reason to use `assert.doesNotThrow()` in the
tests. If a test throws, just let the error bubble up right away
instead of first catching it and then rethrowing it.

PR-URL: nodejs#18669
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
BridgeAR committed Feb 16, 2018
1 parent 4d3c3f0 commit caee112
Show file tree
Hide file tree
Showing 124 changed files with 600 additions and 1,014 deletions.
2 changes: 1 addition & 1 deletion test/addons-napi/test_general/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const y = {};
test_general.wrap(y);
test_general.removeWrap(y);
// Wrapping twice succeeds if a remove_wrap() separates the instances
assert.doesNotThrow(() => test_general.wrap(y));
test_general.wrap(y);

// Ensure that removing a wrap and garbage collecting does not fire the
// finalize callback.
Expand Down
2 changes: 1 addition & 1 deletion test/addons/symlinked-module/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ const sub = require('./submodule');
const mod = require(path.join(i, 'binding.node'));
assert.notStrictEqual(mod, null);
assert.strictEqual(mod.hello(), 'world');
assert.doesNotThrow(() => sub.test(i));
sub.test(i);
});
10 changes: 3 additions & 7 deletions test/internet/test-dgram-membership.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const setup = dgram.createSocket.bind(dgram, { type: 'udp4', reuseAddr: true });
// addMembership() with valid socket and multicast address should not throw
{
const socket = setup();
assert.doesNotThrow(() => { socket.addMembership(multicastAddress); });
socket.addMembership(multicastAddress);
socket.close();
}

Expand All @@ -27,11 +27,7 @@ const setup = dgram.createSocket.bind(dgram, { type: 'udp4', reuseAddr: true });
// dropMembership() after addMembership() should not throw
{
const socket = setup();
assert.doesNotThrow(
() => {
socket.addMembership(multicastAddress);
socket.dropMembership(multicastAddress);
}
);
socket.addMembership(multicastAddress);
socket.dropMembership(multicastAddress);
socket.close();
}
12 changes: 4 additions & 8 deletions test/internet/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,14 +580,10 @@ process.on('exit', function() {
});


assert.doesNotThrow(() =>
dns.lookup(addresses.INET6_HOST, 6, common.mustCall()));
dns.lookup(addresses.INET6_HOST, 6, common.mustCall());

assert.doesNotThrow(() =>
dns.lookup(addresses.INET_HOST, {}, common.mustCall()));
dns.lookup(addresses.INET_HOST, {}, common.mustCall());

assert.doesNotThrow(() =>
dns.lookupService('0.0.0.0', '0', common.mustCall()));
dns.lookupService('0.0.0.0', '0', common.mustCall());

assert.doesNotThrow(() =>
dns.lookupService('0.0.0.0', 0, common.mustCall()));
dns.lookupService('0.0.0.0', 0, common.mustCall());
8 changes: 4 additions & 4 deletions test/parallel/test-assert-checktag.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function re(literals, ...values) {
FakeDate.prototype = Date.prototype;
const fake = new FakeDate();

assert.doesNotThrow(() => assert.deepEqual(date, fake));
assert.doesNotThrow(() => assert.deepEqual(fake, date));
assert.deepEqual(date, fake);
assert.deepEqual(fake, date);

// For deepStrictEqual we check the runtime type,
// then reveal the fakeness of the fake date
Expand All @@ -47,7 +47,7 @@ function re(literals, ...values) {
for (const prop of Object.keys(global)) {
fakeGlobal[prop] = global[prop];
}
assert.doesNotThrow(() => assert.deepEqual(fakeGlobal, global));
assert.deepEqual(fakeGlobal, global);
// Message will be truncated anyway, don't validate
assert.throws(() => assert.deepStrictEqual(fakeGlobal, global),
assert.AssertionError);
Expand All @@ -59,7 +59,7 @@ function re(literals, ...values) {
for (const prop of Object.keys(process)) {
fakeProcess[prop] = process[prop];
}
assert.doesNotThrow(() => assert.deepEqual(fakeProcess, process));
assert.deepEqual(fakeProcess, process);
// Message will be truncated anyway, don't validate
assert.throws(() => assert.deepStrictEqual(fakeProcess, process),
assert.AssertionError);
Expand Down
111 changes: 47 additions & 64 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ const buf = Buffer.from(arr);
// They have different [[Prototype]]
assert.throws(() => assert.deepStrictEqual(arr, buf),
re`${arr} deepStrictEqual ${buf}`);
assert.doesNotThrow(() => assert.deepEqual(arr, buf));
assert.deepEqual(arr, buf);

{
const buf2 = Buffer.from(arr);
buf2.prop = 1;

assert.throws(() => assert.deepStrictEqual(buf2, buf),
re`${buf2} deepStrictEqual ${buf}`);
assert.doesNotThrow(() => assert.deepEqual(buf2, buf));
assert.deepEqual(buf2, buf);
}

{
const arr2 = new Uint8Array([120, 121, 122, 10]);
arr2.prop = 5;
assert.throws(() => assert.deepStrictEqual(arr, arr2),
re`${arr} deepStrictEqual ${arr2}`);
assert.doesNotThrow(() => assert.deepEqual(arr, arr2));
assert.deepEqual(arr, arr2);
}

const date = new Date('2016');
Expand All @@ -66,8 +66,8 @@ const date2 = new MyDate('2016');

// deepEqual returns true as long as the time are the same,
// but deepStrictEqual checks own properties
assert.doesNotThrow(() => assert.deepEqual(date, date2));
assert.doesNotThrow(() => assert.deepEqual(date2, date));
assert.deepEqual(date, date2);
assert.deepEqual(date2, date);
assert.throws(() => assert.deepStrictEqual(date, date2),
re`${date} deepStrictEqual ${date2}`);
assert.throws(() => assert.deepStrictEqual(date2, date),
Expand All @@ -85,7 +85,7 @@ const re2 = new MyRegExp('test');

// deepEqual returns true as long as the regexp-specific properties
// are the same, but deepStrictEqual checks all properties
assert.doesNotThrow(() => assert.deepEqual(re1, re2));
assert.deepEqual(re1, re2);
assert.throws(() => assert.deepStrictEqual(re1, re2),
re`${re1} deepStrictEqual ${re2}`);

Expand Down Expand Up @@ -148,11 +148,11 @@ function assertNotDeepOrStrict(a, b, err) {
}

function assertOnlyDeepEqual(a, b, err) {
assert.doesNotThrow(() => assert.deepEqual(a, b));
assert.deepEqual(a, b);
assert.throws(() => assert.deepStrictEqual(a, b), err ||
re`${a} deepStrictEqual ${b}`);

assert.doesNotThrow(() => assert.deepEqual(b, a));
assert.deepEqual(b, a);
assert.throws(() => assert.deepStrictEqual(b, a), err ||
re`${b} deepStrictEqual ${a}`);
}
Expand Down Expand Up @@ -492,10 +492,9 @@ assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]);

// Handle NaN
assert.throws(() => { assert.deepEqual(NaN, NaN); }, assert.AssertionError);
assert.doesNotThrow(() => { assert.deepStrictEqual(NaN, NaN); });
assert.doesNotThrow(() => { assert.deepStrictEqual({ a: NaN }, { a: NaN }); });
assert.doesNotThrow(
() => { assert.deepStrictEqual([ 1, 2, NaN, 4 ], [ 1, 2, NaN, 4 ]); });
{ assert.deepStrictEqual(NaN, NaN); }
{ assert.deepStrictEqual({ a: NaN }, { a: NaN }); }
assert.deepStrictEqual([ 1, 2, NaN, 4 ], [ 1, 2, NaN, 4 ]);

// Handle boxed primitives
{
Expand Down Expand Up @@ -547,9 +546,7 @@ assertDeepAndStrictEqual(-0, -0);
assertDeepAndStrictEqual(a, b);
}

assert.doesNotThrow(
() => assert.deepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)),
'deepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14))');
assert.deepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14));

assert.throws(() => assert.deepEqual(new Date(), new Date(2000, 3, 14)),
AssertionError,
Expand All @@ -561,16 +558,13 @@ assert.throws(
'notDeepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14))'
);

assert.doesNotThrow(
() => assert.notDeepEqual(new Date(), new Date(2000, 3, 14)),
'notDeepEqual(new Date(), new Date(2000, 3, 14))'
);
assert.notDeepEqual(new Date(), new Date(2000, 3, 14));

assert.doesNotThrow(() => assert.deepEqual(/a/, /a/));
assert.doesNotThrow(() => assert.deepEqual(/a/g, /a/g));
assert.doesNotThrow(() => assert.deepEqual(/a/i, /a/i));
assert.doesNotThrow(() => assert.deepEqual(/a/m, /a/m));
assert.doesNotThrow(() => assert.deepEqual(/a/igm, /a/igm));
assert.deepEqual(/a/, /a/);
assert.deepEqual(/a/g, /a/g);
assert.deepEqual(/a/i, /a/i);
assert.deepEqual(/a/m, /a/m);
assert.deepEqual(/a/igm, /a/igm);
assert.throws(() => assert.deepEqual(/ab/, /a/),
{
code: 'ERR_ASSERTION',
Expand Down Expand Up @@ -605,23 +599,23 @@ assert.throws(() => assert.deepEqual(/a/igm, /a/im),
{
const re1 = /a/g;
re1.lastIndex = 3;
assert.doesNotThrow(() => assert.deepEqual(re1, /a/g));
assert.deepEqual(re1, /a/g);
}

assert.doesNotThrow(() => assert.deepEqual(4, '4'), 'deepEqual(4, \'4\')');
assert.doesNotThrow(() => assert.deepEqual(true, 1), 'deepEqual(true, 1)');
assert.deepEqual(4, '4');
assert.deepEqual(true, 1);
assert.throws(() => assert.deepEqual(4, '5'),
AssertionError,
'deepEqual( 4, \'5\')');

// Having the same number of owned properties && the same set of keys.
assert.doesNotThrow(() => assert.deepEqual({ a: 4 }, { a: 4 }));
assert.doesNotThrow(() => assert.deepEqual({ a: 4, b: '2' }, { a: 4, b: '2' }));
assert.doesNotThrow(() => assert.deepEqual([4], ['4']));
assert.deepEqual({ a: 4 }, { a: 4 });
assert.deepEqual({ a: 4, b: '2' }, { a: 4, b: '2' });
assert.deepEqual([4], ['4']);
assert.throws(
() => assert.deepEqual({ a: 4 }, { a: 4, b: true }), AssertionError);
assert.doesNotThrow(() => assert.deepEqual(['a'], { 0: 'a' }));
assert.doesNotThrow(() => assert.deepEqual({ a: 4, b: '1' }, { b: '1', a: 4 }));
assert.deepEqual(['a'], { 0: 'a' });
assert.deepEqual({ a: 4, b: '1' }, { b: '1', a: 4 });
const a1 = [1, 2, 3];
const a2 = [1, 2, 3];
a1.a = 'test';
Expand All @@ -630,7 +624,7 @@ a2.b = true;
a2.a = 'test';
assert.throws(() => assert.deepEqual(Object.keys(a1), Object.keys(a2)),
AssertionError);
assert.doesNotThrow(() => assert.deepEqual(a1, a2));
assert.deepEqual(a1, a2);

// Having an identical prototype property.
const nbRoot = {
Expand All @@ -654,11 +648,11 @@ nameBuilder2.prototype = nbRoot;
const nb1 = new nameBuilder('Ryan', 'Dahl');
let nb2 = new nameBuilder2('Ryan', 'Dahl');

assert.doesNotThrow(() => assert.deepEqual(nb1, nb2));
assert.deepEqual(nb1, nb2);

nameBuilder2.prototype = Object;
nb2 = new nameBuilder2('Ryan', 'Dahl');
assert.doesNotThrow(() => assert.deepEqual(nb1, nb2));
assert.deepEqual(nb1, nb2);

// Primitives and object.
assert.throws(() => assert.deepEqual(null, {}), AssertionError);
Expand All @@ -670,21 +664,15 @@ assert.throws(() => assert.deepEqual(true, {}), AssertionError);
assert.throws(() => assert.deepEqual(Symbol(), {}), AssertionError);

// Primitive wrappers and object.
assert.doesNotThrow(() => assert.deepEqual(new String('a'), ['a']),
AssertionError);
assert.doesNotThrow(() => assert.deepEqual(new String('a'), { 0: 'a' }),
AssertionError);
assert.doesNotThrow(() => assert.deepEqual(new Number(1), {}), AssertionError);
assert.doesNotThrow(() => assert.deepEqual(new Boolean(true), {}),
AssertionError);
assert.deepEqual(new String('a'), ['a']);
assert.deepEqual(new String('a'), { 0: 'a' });
assert.deepEqual(new Number(1), {});
assert.deepEqual(new Boolean(true), {});

// Same number of keys but different key names.
assert.throws(() => assert.deepEqual({ a: 1 }, { b: 1 }), AssertionError);

assert.doesNotThrow(
() => assert.deepStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)),
'deepStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14))'
);
assert.deepStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14));

assert.throws(
() => assert.deepStrictEqual(new Date(), new Date(2000, 3, 14)),
Expand All @@ -698,16 +686,13 @@ assert.throws(
'notDeepStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14))'
);

assert.doesNotThrow(
() => assert.notDeepStrictEqual(new Date(), new Date(2000, 3, 14)),
'notDeepStrictEqual(new Date(), new Date(2000, 3, 14))'
);
assert.notDeepStrictEqual(new Date(), new Date(2000, 3, 14));

assert.doesNotThrow(() => assert.deepStrictEqual(/a/, /a/));
assert.doesNotThrow(() => assert.deepStrictEqual(/a/g, /a/g));
assert.doesNotThrow(() => assert.deepStrictEqual(/a/i, /a/i));
assert.doesNotThrow(() => assert.deepStrictEqual(/a/m, /a/m));
assert.doesNotThrow(() => assert.deepStrictEqual(/a/igm, /a/igm));
assert.deepStrictEqual(/a/, /a/);
assert.deepStrictEqual(/a/g, /a/g);
assert.deepStrictEqual(/a/i, /a/i);
assert.deepStrictEqual(/a/m, /a/m);
assert.deepStrictEqual(/a/igm, /a/igm);
assert.throws(
() => assert.deepStrictEqual(/ab/, /a/),
{
Expand Down Expand Up @@ -747,7 +732,7 @@ assert.throws(
{
const re1 = /a/;
re1.lastIndex = 3;
assert.doesNotThrow(() => assert.deepStrictEqual(re1, /a/));
assert.deepStrictEqual(re1, /a/);
}

assert.throws(() => assert.deepStrictEqual(4, '4'),
Expand All @@ -763,9 +748,8 @@ assert.throws(() => assert.deepStrictEqual(4, '5'),
'deepStrictEqual(4, \'5\')');

// Having the same number of owned properties && the same set of keys.
assert.doesNotThrow(() => assert.deepStrictEqual({ a: 4 }, { a: 4 }));
assert.doesNotThrow(
() => assert.deepStrictEqual({ a: 4, b: '2' }, { a: 4, b: '2' }));
assert.deepStrictEqual({ a: 4 }, { a: 4 });
assert.deepStrictEqual({ a: 4, b: '2' }, { a: 4, b: '2' });
assert.throws(() => assert.deepStrictEqual([4], ['4']),
{
code: 'ERR_ASSERTION',
Expand All @@ -787,14 +771,13 @@ assert.throws(() => assert.deepStrictEqual(['a'], { 0: 'a' }),

/* eslint-enable */

assert.doesNotThrow(
() => assert.deepStrictEqual({ a: 4, b: '1' }, { b: '1', a: 4 }));
assert.deepStrictEqual({ a: 4, b: '1' }, { b: '1', a: 4 });

assert.throws(
() => assert.deepStrictEqual([0, 1, 2, 'a', 'b'], [0, 1, 2, 'b', 'a']),
AssertionError);

assert.doesNotThrow(() => assert.deepStrictEqual(a1, a2));
assert.deepStrictEqual(a1, a2);

// Prototype check.
function Constructor1(first, last) {
Expand All @@ -815,7 +798,7 @@ assert.throws(() => assert.deepStrictEqual(obj1, obj2), AssertionError);
Constructor2.prototype = Constructor1.prototype;
obj2 = new Constructor2('Ryan', 'Dahl');

assert.doesNotThrow(() => assert.deepStrictEqual(obj1, obj2));
assert.deepStrictEqual(obj1, obj2);

// primitives
assert.throws(() => assert.deepStrictEqual(4, '4'), AssertionError);
Expand All @@ -824,7 +807,7 @@ assert.throws(() => assert.deepStrictEqual(Symbol(), Symbol()),
AssertionError);

const s = Symbol();
assert.doesNotThrow(() => assert.deepStrictEqual(s, s));
assert.deepStrictEqual(s, s);

// Primitives and object.
assert.throws(() => assert.deepStrictEqual(null, {}), AssertionError);
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-assert-if-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ assert.throws(
}
);

assert.doesNotThrow(() => { assert.ifError(null); });
assert.doesNotThrow(() => { assert.ifError(); });
assert.doesNotThrow(() => { assert.ifError(undefined); });
assert.ifError(null);
assert.ifError();
assert.ifError(undefined);

// https://github.com/nodejs/node-v0.x-archive/issues/2893
{
Expand Down
Loading

0 comments on commit caee112

Please sign in to comment.