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.

Backport-PR-URL: #19244
PR-URL: #18669
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
BridgeAR authored and MylesBorins committed Mar 21, 2018
1 parent 5478746 commit 574d061
Show file tree
Hide file tree
Showing 123 changed files with 584 additions and 994 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
86 changes: 35 additions & 51 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,10 +558,7 @@ 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.deepEqual(/a/, /a/);
assert.deepEqual(/a/g, /a/g);
Expand Down Expand Up @@ -605,23 +599,23 @@ common.expectsError(() => 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,10 +686,7 @@ 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.deepStrictEqual(/a/, /a/);
assert.deepStrictEqual(/a/g, /a/g);
Expand Down Expand Up @@ -747,7 +732,7 @@ common.expectsError(
{
const re1 = /a/;
re1.lastIndex = 3;
assert.doesNotThrow(() => assert.deepStrictEqual(re1, /a/));
assert.deepStrictEqual(re1, /a/);
}

assert.throws(() => assert.deepStrictEqual(4, '4'),
Expand Down Expand Up @@ -786,14 +771,13 @@ common.expectsError(() => 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 @@ -814,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 @@ -823,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
36 changes: 13 additions & 23 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,22 @@ assert.ok(a.AssertionError.prototype instanceof Error,
'a.AssertionError instanceof Error');

assert.throws(() => a(false), a.AssertionError, 'ok(false)');

assert.doesNotThrow(() => a(true), a.AssertionError, 'ok(true)');

assert.doesNotThrow(() => a('test', 'ok(\'test\')'));

assert.throws(() => a.ok(false), a.AssertionError, 'ok(false)');

assert.doesNotThrow(() => a.ok(true), a.AssertionError, 'ok(true)');

assert.doesNotThrow(() => a.ok('test'), 'ok(\'test\')');
a(true);
a('test', 'ok(\'test\')');
a.ok(true);
a.ok('test');

assert.throws(() => a.equal(true, false),
a.AssertionError, 'equal(true, false)');

assert.doesNotThrow(() => a.equal(null, null), 'equal(null, null)');

assert.doesNotThrow(() => a.equal(undefined, undefined),
'equal(undefined, undefined)');

assert.doesNotThrow(() => a.equal(null, undefined), 'equal(null, undefined)');

assert.doesNotThrow(() => a.equal(true, true), 'equal(true, true)');

assert.doesNotThrow(() => a.equal(2, '2'), 'equal(2, \'2\')');

assert.doesNotThrow(() => a.notEqual(true, false), 'notEqual(true, false)');
a.equal(null, null);
a.equal(undefined, undefined);
a.equal(null, undefined);
a.equal(true, true);
a.equal(2, '2');
a.notEqual(true, false);

assert.throws(() => a.notEqual(true, true),
a.AssertionError, 'notEqual(true, true)');
Expand All @@ -71,7 +61,7 @@ assert.throws(() => a.strictEqual(null, undefined),
assert.throws(() => a.notStrictEqual(2, 2),
a.AssertionError, 'notStrictEqual(2, 2)');

assert.doesNotThrow(() => a.notStrictEqual(2, '2'), 'notStrictEqual(2, \'2\')');
a.notStrictEqual(2, '2');

// Testing the throwing.
function thrower(errorConstructor) {
Expand Down Expand Up @@ -126,8 +116,8 @@ assert.throws(() => thrower(TypeError));

assert.throws(() => { assert.ifError(new Error('test error')); },
/^Error: test error$/);
assert.doesNotThrow(() => { assert.ifError(null); });
assert.doesNotThrow(() => { assert.ifError(); });
assert.ifError(null);
assert.ifError();

common.expectsError(
() => assert.doesNotThrow(() => thrower(Error), 'user message'),
Expand Down
Loading

0 comments on commit 574d061

Please sign in to comment.