Skip to content

Commit f399667

Browse files
mithunsasidharanMylesBorins
authored andcommitted
test: replace assert.throws w/ common.expectsError
PR-URL: #17497 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent c2ff36e commit f399667

11 files changed

+47
-51
lines changed

test/parallel/test-dns.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ const goog = [
6868
];
6969
assert.doesNotThrow(() => dns.setServers(goog));
7070
assert.deepStrictEqual(dns.getServers(), goog);
71-
assert.throws(() => dns.setServers(['foobar']), common.expectsError({
71+
common.expectsError(() => dns.setServers(['foobar']), {
7272
code: 'ERR_INVALID_IP_ADDRESS',
7373
type: Error,
7474
message: 'Invalid IP address: foobar'
75-
}));
76-
assert.throws(() => dns.setServers(['127.0.0.1:va']), common.expectsError({
75+
});
76+
common.expectsError(() => dns.setServers(['127.0.0.1:va']), {
7777
code: 'ERR_INVALID_IP_ADDRESS',
7878
type: Error,
7979
message: 'Invalid IP address: 127.0.0.1:va'
80-
}));
80+
});
8181
assert.deepStrictEqual(dns.getServers(), goog);
8282

8383
const goog6 = [
@@ -109,14 +109,14 @@ assert.deepStrictEqual(dns.getServers(), portsExpected);
109109
assert.doesNotThrow(() => dns.setServers([]));
110110
assert.deepStrictEqual(dns.getServers(), []);
111111

112-
assert.throws(() => {
112+
common.expectsError(() => {
113113
dns.resolve('example.com', [], common.mustNotCall());
114-
}, common.expectsError({
114+
}, {
115115
code: 'ERR_INVALID_ARG_TYPE',
116116
type: TypeError,
117117
message: 'The "rrtype" argument must be of type string. ' +
118118
'Received type object'
119-
}));
119+
});
120120

121121
// dns.lookup should accept only falsey and string values
122122
{
@@ -167,24 +167,24 @@ assert.throws(() => {
167167
* - it's an odd number different than 1, and thus is invalid, because
168168
* flags are either === 1 or even.
169169
*/
170-
assert.throws(() => {
170+
common.expectsError(() => {
171171
dns.lookup('nodejs.org', { hints: (dns.V4MAPPED | dns.ADDRCONFIG) + 1 },
172172
common.mustNotCall());
173-
}, common.expectsError({
173+
}, {
174174
code: 'ERR_INVALID_OPT_VALUE',
175175
type: TypeError,
176176
message: /The value "\d+" is invalid for option "hints"/
177-
}));
177+
});
178178

179-
assert.throws(() => dns.lookup('nodejs.org'), common.expectsError({
179+
common.expectsError(() => dns.lookup('nodejs.org'), {
180180
code: 'ERR_INVALID_CALLBACK',
181181
type: TypeError
182-
}));
182+
});
183183

184-
assert.throws(() => dns.lookup('nodejs.org', 4), common.expectsError({
184+
common.expectsError(() => dns.lookup('nodejs.org', 4), {
185185
code: 'ERR_INVALID_CALLBACK',
186186
type: TypeError
187-
}));
187+
});
188188

189189
assert.doesNotThrow(() => dns.lookup('', { family: 4, hints: 0 },
190190
common.mustCall()));

test/parallel/test-fs-realpath.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ function test_cyclic_link_protection(realpath, realpathSync, callback) {
225225
fs.symlinkSync(t[1], t[0], 'dir');
226226
unlink.push(t[0]);
227227
});
228-
assert.throws(() => {
228+
common.expectsError(() => {
229229
realpathSync(entry);
230-
}, common.expectsError({ code: 'ELOOP', type: Error }));
230+
}, { code: 'ELOOP', type: Error });
231231
asynctest(
232232
realpath, [entry], callback, common.mustCall(function(err, result) {
233233
assert.strictEqual(err.path, entry);

test/parallel/test-fs-watchfile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ common.expectsError(
2424
type: TypeError
2525
});
2626

27-
assert.throws(function() {
27+
common.expectsError(function() {
2828
fs.watchFile(new Object(), common.mustNotCall());
29-
}, common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }));
29+
}, { code: 'ERR_INVALID_ARG_TYPE', type: TypeError });
3030

3131
const enoentFile = path.join(common.tmpDir, 'non-existent-file');
3232
const expectedStatObject = new fs.Stats(

test/parallel/test-http-client-check-http-token.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 http = require('http');
54
const Countdown = require('../common/countdown');
65

@@ -18,14 +17,14 @@ const server = http.createServer(common.mustCall((req, res) => {
1817

1918
server.listen(0, common.mustCall(() => {
2019
expectedFails.forEach((method) => {
21-
assert.throws(() => {
20+
common.expectsError(() => {
2221
http.request({ method, path: '/' }, common.mustNotCall());
23-
}, common.expectsError({
22+
}, {
2423
code: 'ERR_INVALID_ARG_TYPE',
2524
type: TypeError,
2625
message: 'The "method" argument must be of type string. ' +
2726
`Received type ${typeof method}`
28-
}));
27+
});
2928
});
3029

3130
expectedSuccesses.forEach((method) => {

test/parallel/test-http-client-reject-unexpected-agent.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ server.listen(0, baseOptions.host, common.mustCall(function() {
4747
baseOptions.port = this.address().port;
4848

4949
failingAgentOptions.forEach((agent) => {
50-
assert.throws(
50+
common.expectsError(
5151
() => createRequest(agent),
52-
common.expectsError({
52+
{
5353
code: 'ERR_INVALID_ARG_TYPE',
5454
type: TypeError,
5555
message: 'The "Agent option" argument must be one of type ' +
5656
'Agent-like Object, undefined, or false'
57-
})
57+
}
5858
);
5959
});
6060

test/parallel/test-http-client-unescaped-path.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@
2121

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

2726
for (let i = 0; i <= 32; i += 1) {
2827
const path = `bad${String.fromCharCode(i)}path`;
29-
assert.throws(
28+
common.expectsError(
3029
() => http.get({ path }, common.mustNotCall()),
31-
common.expectsError({
30+
{
3231
code: 'ERR_UNESCAPED_CHARACTERS',
3332
type: TypeError,
3433
message: 'Request path contains unescaped characters'
35-
})
34+
}
3635
);
3736
}

test/parallel/test-http-hostname-typechecking.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ const http = require('http');
99
const vals = [{}, [], NaN, Infinity, -Infinity, true, false, 1, 0, new Date()];
1010

1111
vals.forEach((v) => {
12-
assert.throws(
12+
common.expectsError(
1313
() => http.request({ hostname: v }),
14-
common.expectsError({
14+
{
1515
code: 'ERR_INVALID_ARG_TYPE',
1616
type: TypeError,
1717
message: 'The "options.hostname" property must be one of ' +
1818
'type string, undefined, or null. ' +
1919
`Received type ${typeof v}`
20-
})
20+
}
2121
);
2222

23-
assert.throws(
23+
common.expectsError(
2424
() => http.request({ host: v }),
25-
common.expectsError({
25+
{
2626
code: 'ERR_INVALID_ARG_TYPE',
2727
type: TypeError,
2828
message: 'The "options.host" property must be one of ' +
2929
'type string, undefined, or null. ' +
3030
`Received type ${typeof v}`
31-
})
31+
}
3232
);
3333
});
3434

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
'use strict';
22
const common = require('../common');
3-
const assert = require('assert');
43
const http = require('http');
54

6-
assert.throws(
5+
common.expectsError(
76
() => http.request({ method: '\0' }),
8-
common.expectsError({
7+
{
98
code: 'ERR_INVALID_HTTP_TOKEN',
109
type: TypeError,
1110
message: 'Method must be a valid HTTP token ["\u0000"]'
12-
})
11+
}
1312
);

test/parallel/test-http-response-statuscode.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ const server = http.Server(common.mustCall(function(req, res) {
5656
test(res, '404 this is not valid either', '404 this is not valid either');
5757
break;
5858
case 12:
59-
assert.throws(() => { res.writeHead(); },
60-
common.expectsError({
61-
code: 'ERR_HTTP_INVALID_STATUS_CODE',
62-
type: RangeError,
63-
message: 'Invalid status code: undefined'
64-
}));
59+
common.expectsError(() => { res.writeHead(); },
60+
{
61+
code: 'ERR_HTTP_INVALID_STATUS_CODE',
62+
type: RangeError,
63+
message: 'Invalid status code: undefined'
64+
});
6565
this.close();
6666
break;
6767
default:

test/parallel/test-http-server-de-chunked-trailer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const server = http.createServer(common.mustCall(function(req, res) {
1414
message: 'Trailers are invalid with this transfer encoding',
1515
type: Error
1616
};
17-
assert.throws(() => res.writeHead(200, { 'Content-Length': '2' }),
18-
common.expectsError(trailerInvalidErr));
17+
common.expectsError(() => res.writeHead(200, { 'Content-Length': '2' }),
18+
trailerInvalidErr);
1919
res.removeHeader('Trailer');
2020
res.end('ok');
2121
}));

test/parallel/test-http-write-head.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,13 @@ const s = http.createServer(common.mustCall((req, res) => {
5252

5353
res.writeHead(200, { Test: '2' });
5454

55-
assert.throws(() => {
55+
common.expectsError(() => {
5656
res.writeHead(100, {});
57-
}, common.expectsError({
57+
}, {
5858
code: 'ERR_HTTP_HEADERS_SENT',
5959
type: Error,
6060
message: 'Cannot render headers after they are sent to the client'
61-
})
62-
);
61+
});
6362

6463
res.end();
6564
}));

0 commit comments

Comments
 (0)