Skip to content

Commit c2ff36e

Browse files
mithunsasidharanMylesBorins
authored andcommitted
test: replace assert.throws w/ common.expectsError
PR-URL: #17494 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent af8e27d commit c2ff36e

10 files changed

+85
-88
lines changed

test/parallel/test-dgram-createSocket-type.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ const errMessage = /^Bad socket type specified\. Valid types are: udp4, udp6$/;
2323

2424
// Error must be thrown with invalid types
2525
invalidTypes.forEach((invalidType) => {
26-
assert.throws(() => {
26+
common.expectsError(() => {
2727
dgram.createSocket(invalidType);
28-
}, common.expectsError({
28+
}, {
2929
code: 'ERR_SOCKET_BAD_TYPE',
3030
type: TypeError,
3131
message: errMessage
32-
}));
32+
});
3333
});
3434

3535
// Error must not be thrown with valid types

test/parallel/test-dgram-custom-lookup.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22
const common = require('../common');
3-
const assert = require('assert');
43
const dgram = require('dgram');
54
const dns = require('dns');
65

@@ -36,12 +35,12 @@ const dns = require('dns');
3635
{
3736
// Verify that non-functions throw.
3837
[null, true, false, 0, 1, NaN, '', 'foo', {}, Symbol()].forEach((value) => {
39-
assert.throws(() => {
38+
common.expectsError(() => {
4039
dgram.createSocket({ type: 'udp4', lookup: value });
41-
}, common.expectsError({
40+
}, {
4241
code: 'ERR_INVALID_ARG_TYPE',
4342
type: TypeError,
4443
message: 'The "lookup" argument must be of type Function'
45-
}));
44+
});
4645
});
4746
}

test/parallel/test-dgram-membership.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,53 @@ const setup = dgram.createSocket.bind(dgram, { type: 'udp4', reuseAddr: true });
1111
{
1212
const socket = setup();
1313
socket.close(common.mustCall(() => {
14-
assert.throws(() => {
14+
common.expectsError(() => {
1515
socket.addMembership(multicastAddress);
16-
}, common.expectsError({
16+
}, {
1717
code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
1818
type: Error,
1919
message: /^Not running$/
20-
}));
20+
});
2121
}));
2222
}
2323

2424
// dropMembership() on closed socket should throw
2525
{
2626
const socket = setup();
2727
socket.close(common.mustCall(() => {
28-
assert.throws(() => {
28+
common.expectsError(() => {
2929
socket.dropMembership(multicastAddress);
30-
}, common.expectsError({
30+
}, {
3131
code: 'ERR_SOCKET_DGRAM_NOT_RUNNING',
3232
type: Error,
3333
message: /^Not running$/
34-
}));
34+
});
3535
}));
3636
}
3737

3838
// addMembership() with no argument should throw
3939
{
4040
const socket = setup();
41-
assert.throws(() => {
41+
common.expectsError(() => {
4242
socket.addMembership();
43-
}, common.expectsError({
43+
}, {
4444
code: 'ERR_MISSING_ARGS',
4545
type: TypeError,
4646
message: /^The "multicastAddress" argument must be specified$/
47-
}));
47+
});
4848
socket.close();
4949
}
5050

5151
// dropMembership() with no argument should throw
5252
{
5353
const socket = setup();
54-
assert.throws(() => {
54+
common.expectsError(() => {
5555
socket.dropMembership();
56-
}, common.expectsError({
56+
}, {
5757
code: 'ERR_MISSING_ARGS',
5858
type: TypeError,
5959
message: /^The "multicastAddress" argument must be specified$/
60-
}));
60+
});
6161
socket.close();
6262
}
6363

test/parallel/test-dgram-multicast-setTTL.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ socket.on('listening', common.mustCall(() => {
3535
socket.setMulticastTTL(1000);
3636
}, /^Error: setMulticastTTL EINVAL$/);
3737

38-
assert.throws(() => {
38+
common.expectsError(() => {
3939
socket.setMulticastTTL('foo');
40-
}, common.expectsError({
40+
}, {
4141
code: 'ERR_INVALID_ARG_TYPE',
4242
type: TypeError,
4343
message: 'The "ttl" argument must be of type number. Received type string'
44-
}));
44+
});
4545

4646
//close the socket
4747
socket.close();

test/parallel/test-dgram-send-address-types.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ const client = dgram.createSocket('udp4').bind(0, () => {
3838
client.send(buf, port, onMessage);
3939

4040
// invalid address: object
41-
assert.throws(() => {
41+
common.expectsError(() => {
4242
client.send(buf, port, []);
43-
}, common.expectsError(expectedError));
43+
}, expectedError);
4444

4545
// invalid address: nonzero number
46-
assert.throws(() => {
46+
common.expectsError(() => {
4747
client.send(buf, port, 1);
48-
}, common.expectsError(expectedError));
48+
}, expectedError);
4949

5050
// invalid address: true
51-
assert.throws(() => {
51+
common.expectsError(() => {
5252
client.send(buf, port, true);
53-
}, common.expectsError(expectedError));
53+
}, expectedError);
5454
});
5555

5656
client.unref();

test/parallel/test-dgram-sendto.js

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
'use strict';
22
const common = require('../common');
3-
const assert = require('assert');
43
const dgram = require('dgram');
54
const socket = dgram.createSocket('udp4');
65

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

10-
assert.throws(() => {
9+
common.expectsError(() => {
1110
socket.sendto();
12-
}, common.expectsError({
11+
}, {
1312
code: 'ERR_INVALID_ARG_TYPE',
1413
type: TypeError,
1514
message: errorMessageOffset
16-
}));
15+
});
1716

18-
assert.throws(() => {
17+
common.expectsError(() => {
1918
socket.sendto('buffer', 1, 'offset', 'port', 'address', 'cb');
20-
}, common.expectsError({
19+
}, {
2120
code: 'ERR_INVALID_ARG_TYPE',
2221
type: TypeError,
2322
message: /^The "length" argument must be of type number$/
24-
}));
23+
});
2524

26-
assert.throws(() => {
25+
common.expectsError(() => {
2726
socket.sendto('buffer', 'offset', 1, 'port', 'address', 'cb');
28-
}, common.expectsError({
27+
}, {
2928
code: 'ERR_INVALID_ARG_TYPE',
3029
type: TypeError,
3130
message: errorMessageOffset
32-
}));
31+
});
3332

34-
assert.throws(() => {
33+
common.expectsError(() => {
3534
socket.sendto('buffer', 1, 1, 10, false, 'cb');
36-
}, common.expectsError({
35+
}, {
3736
code: 'ERR_INVALID_ARG_TYPE',
3837
type: TypeError,
3938
message: /^The "address" argument must be of type string$/
40-
}));
39+
});
4140

42-
assert.throws(() => {
41+
common.expectsError(() => {
4342
socket.sendto('buffer', 1, 1, false, 'address', 'cb');
44-
}, common.expectsError({
43+
}, {
4544
code: 'ERR_INVALID_ARG_TYPE',
4645
type: TypeError,
4746
message: /^The "port" argument must be of type number$/
48-
}));
47+
});

test/parallel/test-dgram-setTTL.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ socket.on('listening', common.mustCall(() => {
99
const result = socket.setTTL(16);
1010
assert.strictEqual(result, 16);
1111

12-
assert.throws(() => {
12+
common.expectsError(() => {
1313
socket.setTTL('foo');
14-
}, common.expectsError({
14+
}, {
1515
code: 'ERR_INVALID_ARG_TYPE',
1616
type: TypeError,
1717
message: 'The "ttl" argument must be of type number. Received type string'
18-
}));
18+
});
1919

2020
// TTL must be a number from > 0 to < 256
2121
assert.throws(() => {

test/parallel/test-dgram-socket-buffer-size.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ const dgram = require('dgram');
1414

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

17-
assert.throws(() => {
17+
common.expectsError(() => {
1818
socket.setRecvBufferSize(8192);
19-
}, common.expectsError(errorObj));
19+
}, errorObj);
2020

21-
assert.throws(() => {
21+
common.expectsError(() => {
2222
socket.setSendBufferSize(8192);
23-
}, common.expectsError(errorObj));
23+
}, errorObj);
2424

25-
assert.throws(() => {
25+
common.expectsError(() => {
2626
socket.getRecvBufferSize();
27-
}, common.expectsError(errorObj));
27+
}, errorObj);
2828

29-
assert.throws(() => {
29+
common.expectsError(() => {
3030
socket.getSendBufferSize();
31-
}, common.expectsError(errorObj));
31+
}, errorObj);
3232
}
3333

3434
{
@@ -45,13 +45,13 @@ const dgram = require('dgram');
4545

4646
socket.bind(common.mustCall(() => {
4747
badBufferSizes.forEach((badBufferSize) => {
48-
assert.throws(() => {
48+
common.expectsError(() => {
4949
socket.setRecvBufferSize(badBufferSize);
50-
}, common.expectsError(errorObj));
50+
}, errorObj);
5151

52-
assert.throws(() => {
52+
common.expectsError(() => {
5353
socket.setSendBufferSize(badBufferSize);
54-
}, common.expectsError(errorObj));
54+
}, errorObj);
5555
});
5656
socket.close();
5757
}));
@@ -84,9 +84,9 @@ function checkBufferSizeError(type, size) {
8484
'BufferSize';
8585
const socket = dgram.createSocket('udp4');
8686
socket.bind(common.mustCall(() => {
87-
assert.throws(() => {
87+
common.expectsError(() => {
8888
socket[functionName](size);
89-
}, common.expectsError(errorObj));
89+
}, errorObj);
9090
socket.close();
9191
}));
9292
}

test/parallel/test-dns-lookup.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,51 @@ const dns = require('dns');
77
// Stub `getaddrinfo` to *always* error.
88
cares.getaddrinfo = () => process.binding('uv').UV_ENOENT;
99

10-
assert.throws(() => {
10+
common.expectsError(() => {
1111
dns.lookup(1, {});
12-
}, common.expectsError({
12+
}, {
1313
code: 'ERR_INVALID_ARG_TYPE',
1414
type: TypeError,
1515
message: /^The "hostname" argument must be one of type string or falsy/
16-
}));
16+
});
1717

18-
assert.throws(() => {
18+
common.expectsError(() => {
1919
dns.lookup(false, 'cb');
20-
}, common.expectsError({
20+
}, {
2121
code: 'ERR_INVALID_CALLBACK',
2222
type: TypeError
23-
}));
23+
});
2424

25-
assert.throws(() => {
25+
common.expectsError(() => {
2626
dns.lookup(false, 'options', 'cb');
27-
}, common.expectsError({
27+
}, {
2828
code: 'ERR_INVALID_CALLBACK',
2929
type: TypeError
30-
}));
30+
});
3131

32-
assert.throws(() => {
32+
common.expectsError(() => {
3333
dns.lookup(false, {
3434
hints: 100,
3535
family: 0,
3636
all: false
3737
}, common.mustNotCall());
38-
}, common.expectsError({
38+
}, {
3939
code: 'ERR_INVALID_OPT_VALUE',
4040
type: TypeError,
4141
message: 'The value "100" is invalid for option "hints"'
42-
}));
42+
});
4343

44-
assert.throws(() => {
44+
common.expectsError(() => {
4545
dns.lookup(false, {
4646
hints: 0,
4747
family: 20,
4848
all: false
4949
}, common.mustNotCall());
50-
}, common.expectsError({
50+
}, {
5151
code: 'ERR_INVALID_OPT_VALUE',
5252
type: TypeError,
5353
message: 'The value "20" is invalid for option "family"'
54-
}));
54+
});
5555

5656
assert.doesNotThrow(() => {
5757
dns.lookup(false, {

test/parallel/test-dns-regress-7070.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@
2121

2222
'use strict';
2323
const common = require('../common');
24-
const assert = require('assert');
2524
const dns = require('dns');
2625

2726
// Should not raise assertion error. Issue #7070
28-
assert.throws(() => dns.resolveNs([]), // bad name
29-
common.expectsError({
30-
code: 'ERR_INVALID_ARG_TYPE',
31-
type: TypeError,
32-
message: /^The "name" argument must be of type string/
33-
}));
34-
assert.throws(() => dns.resolveNs(''), // bad callback
35-
common.expectsError({
36-
code: 'ERR_INVALID_CALLBACK',
37-
type: TypeError
38-
}));
27+
common.expectsError(() => dns.resolveNs([]), // bad name
28+
{
29+
code: 'ERR_INVALID_ARG_TYPE',
30+
type: TypeError,
31+
message: /^The "name" argument must be of type string/
32+
});
33+
common.expectsError(() => dns.resolveNs(''), // bad callback
34+
{
35+
code: 'ERR_INVALID_CALLBACK',
36+
type: TypeError
37+
});

0 commit comments

Comments
 (0)