Skip to content

test: replace assert.throws with common.expectsError #17494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions test/parallel/test-dgram-createSocket-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ const errMessage = /^Bad socket type specified\. Valid types are: udp4, udp6$/;

// Error must be thrown with invalid types
invalidTypes.forEach((invalidType) => {
assert.throws(() => {
common.expectsError(() => {
dgram.createSocket(invalidType);
}, common.expectsError({
}, {
code: 'ERR_SOCKET_BAD_TYPE',
type: TypeError,
message: errMessage
}));
});
});

// Error must not be thrown with valid types
Expand Down
7 changes: 3 additions & 4 deletions test/parallel/test-dgram-custom-lookup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const dns = require('dns');

Expand Down Expand Up @@ -36,12 +35,12 @@ const dns = require('dns');
{
// Verify that non-functions throw.
[null, true, false, 0, 1, NaN, '', 'foo', {}, Symbol()].forEach((value) => {
assert.throws(() => {
common.expectsError(() => {
dgram.createSocket({ type: 'udp4', lookup: value });
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "lookup" argument must be of type Function'
}));
});
});
}
24 changes: 12 additions & 12 deletions test/parallel/test-dgram-membership.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,53 @@ const setup = dgram.createSocket.bind(dgram, { type: 'udp4', reuseAddr: true });
{
const socket = setup();
socket.close(common.mustCall(() => {
assert.throws(() => {
common.expectsError(() => {
socket.addMembership(multicastAddress);
}, common.expectsError({
}, {
code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
type: Error,
message: /^Not running$/
}));
});
}));
}

// dropMembership() on closed socket should throw
{
const socket = setup();
socket.close(common.mustCall(() => {
assert.throws(() => {
common.expectsError(() => {
socket.dropMembership(multicastAddress);
}, common.expectsError({
}, {
code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
type: Error,
message: /^Not running$/
}));
});
}));
}

// addMembership() with no argument should throw
{
const socket = setup();
assert.throws(() => {
common.expectsError(() => {
socket.addMembership();
}, common.expectsError({
}, {
code: 'ERR_MISSING_ARGS',
type: TypeError,
message: /^The "multicastAddress" argument must be specified$/
}));
});
socket.close();
}

// dropMembership() with no argument should throw
{
const socket = setup();
assert.throws(() => {
common.expectsError(() => {
socket.dropMembership();
}, common.expectsError({
}, {
code: 'ERR_MISSING_ARGS',
type: TypeError,
message: /^The "multicastAddress" argument must be specified$/
}));
});
socket.close();
}

Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-dgram-multicast-setTTL.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ socket.on('listening', common.mustCall(() => {
socket.setMulticastTTL(1000);
}, /^Error: setMulticastTTL EINVAL$/);

assert.throws(() => {
common.expectsError(() => {
socket.setMulticastTTL('foo');
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "ttl" argument must be of type number. Received type string'
}));
});

//close the socket
socket.close();
Expand Down
12 changes: 6 additions & 6 deletions test/parallel/test-dgram-send-address-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ const client = dgram.createSocket('udp4').bind(0, () => {
client.send(buf, port, onMessage);

// invalid address: object
assert.throws(() => {
common.expectsError(() => {
client.send(buf, port, []);
}, common.expectsError(expectedError));
}, expectedError);

// invalid address: nonzero number
assert.throws(() => {
common.expectsError(() => {
client.send(buf, port, 1);
}, common.expectsError(expectedError));
}, expectedError);

// invalid address: true
assert.throws(() => {
common.expectsError(() => {
client.send(buf, port, true);
}, common.expectsError(expectedError));
}, expectedError);
});

client.unref();
31 changes: 15 additions & 16 deletions test/parallel/test-dgram-sendto.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const socket = dgram.createSocket('udp4');

const errorMessageOffset =
/^The "offset" argument must be of type number$/;

assert.throws(() => {
common.expectsError(() => {
socket.sendto();
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: errorMessageOffset
}));
});

assert.throws(() => {
common.expectsError(() => {
socket.sendto('buffer', 1, 'offset', 'port', 'address', 'cb');
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "length" argument must be of type number$/
}));
});

assert.throws(() => {
common.expectsError(() => {
socket.sendto('buffer', 'offset', 1, 'port', 'address', 'cb');
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: errorMessageOffset
}));
});

assert.throws(() => {
common.expectsError(() => {
socket.sendto('buffer', 1, 1, 10, false, 'cb');
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "address" argument must be of type string$/
}));
});

assert.throws(() => {
common.expectsError(() => {
socket.sendto('buffer', 1, 1, false, 'address', 'cb');
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "port" argument must be of type number$/
}));
});
6 changes: 3 additions & 3 deletions test/parallel/test-dgram-setTTL.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ socket.on('listening', common.mustCall(() => {
const result = socket.setTTL(16);
assert.strictEqual(result, 16);

assert.throws(() => {
common.expectsError(() => {
socket.setTTL('foo');
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "ttl" argument must be of type number. Received type string'
}));
});

// TTL must be a number from > 0 to < 256
assert.throws(() => {
Expand Down
28 changes: 14 additions & 14 deletions test/parallel/test-dgram-socket-buffer-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ const dgram = require('dgram');

const socket = dgram.createSocket('udp4');

assert.throws(() => {
common.expectsError(() => {
socket.setRecvBufferSize(8192);
}, common.expectsError(errorObj));
}, errorObj);

assert.throws(() => {
common.expectsError(() => {
socket.setSendBufferSize(8192);
}, common.expectsError(errorObj));
}, errorObj);

assert.throws(() => {
common.expectsError(() => {
socket.getRecvBufferSize();
}, common.expectsError(errorObj));
}, errorObj);

assert.throws(() => {
common.expectsError(() => {
socket.getSendBufferSize();
}, common.expectsError(errorObj));
}, errorObj);
}

{
Expand All @@ -45,13 +45,13 @@ const dgram = require('dgram');

socket.bind(common.mustCall(() => {
badBufferSizes.forEach((badBufferSize) => {
assert.throws(() => {
common.expectsError(() => {
socket.setRecvBufferSize(badBufferSize);
}, common.expectsError(errorObj));
}, errorObj);

assert.throws(() => {
common.expectsError(() => {
socket.setSendBufferSize(badBufferSize);
}, common.expectsError(errorObj));
}, errorObj);
});
socket.close();
}));
Expand Down Expand Up @@ -83,9 +83,9 @@ function checkBufferSizeError(type, size) {
'BufferSize';
const socket = dgram.createSocket('udp4');
socket.bind(common.mustCall(() => {
assert.throws(() => {
common.expectsError(() => {
socket[functionName](size);
}, common.expectsError(errorObj));
}, errorObj);
socket.close();
}));
}
Expand Down
30 changes: 15 additions & 15 deletions test/parallel/test-dns-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,51 @@ const dns = require('dns');
// Stub `getaddrinfo` to *always* error.
cares.getaddrinfo = () => process.binding('uv').UV_ENOENT;

assert.throws(() => {
common.expectsError(() => {
dns.lookup(1, {});
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "hostname" argument must be one of type string or falsy/
}));
});

assert.throws(() => {
common.expectsError(() => {
dns.lookup(false, 'cb');
}, common.expectsError({
}, {
code: 'ERR_INVALID_CALLBACK',
type: TypeError
}));
});

assert.throws(() => {
common.expectsError(() => {
dns.lookup(false, 'options', 'cb');
}, common.expectsError({
}, {
code: 'ERR_INVALID_CALLBACK',
type: TypeError
}));
});

assert.throws(() => {
common.expectsError(() => {
dns.lookup(false, {
hints: 100,
family: 0,
all: false
}, common.mustNotCall());
}, common.expectsError({
}, {
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError,
message: 'The value "100" is invalid for option "hints"'
}));
});

assert.throws(() => {
common.expectsError(() => {
dns.lookup(false, {
hints: 0,
family: 20,
all: false
}, common.mustNotCall());
}, common.expectsError({
}, {
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError,
message: 'The value "20" is invalid for option "family"'
}));
});

assert.doesNotThrow(() => {
dns.lookup(false, {
Expand Down
23 changes: 11 additions & 12 deletions test/parallel/test-dns-regress-7070.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@

'use strict';
const common = require('../common');
const assert = require('assert');
const dns = require('dns');

// Should not raise assertion error. Issue #7070
assert.throws(() => dns.resolveNs([]), // bad name
common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "name" argument must be of type string/
}));
assert.throws(() => dns.resolveNs(''), // bad callback
common.expectsError({
code: 'ERR_INVALID_CALLBACK',
type: TypeError
}));
common.expectsError(() => dns.resolveNs([]), // bad name
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "name" argument must be of type string/
});
common.expectsError(() => dns.resolveNs(''), // bad callback
{
code: 'ERR_INVALID_CALLBACK',
type: TypeError
});