Skip to content

Commit ff59d3a

Browse files
mithunsasidharanMylesBorins
authored andcommitted
test: replace assert.throws w/ common.expectsError
PR-URL: #17483 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 28b2d8a commit ff59d3a

10 files changed

+66
-62
lines changed

‎test/parallel/test-async-hooks-asyncresource-constructor.js‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// This tests that AsyncResource throws an error if bad parameters are passed
44

55
constcommon=require('../common');
6-
constassert=require('assert');
76
constasync_hooks=require('async_hooks');
87
const{ AsyncResource }=async_hooks;
98

@@ -12,30 +11,30 @@ async_hooks.createHook({
1211
init(){}
1312
}).enable();
1413

15-
assert.throws(()=>{
14+
common.expectsError(()=>{
1615
returnnewAsyncResource();
17-
},common.expectsError({
16+
},{
1817
code: 'ERR_INVALID_ARG_TYPE',
1918
type: TypeError,
20-
}));
19+
});
2120

22-
assert.throws(()=>{
21+
common.expectsError(()=>{
2322
newAsyncResource('');
24-
},common.expectsError({
23+
},{
2524
code: 'ERR_ASYNC_TYPE',
2625
type: TypeError,
27-
}));
26+
});
2827

29-
assert.throws(()=>{
28+
common.expectsError(()=>{
3029
newAsyncResource('type',-4);
31-
},common.expectsError({
30+
},{
3231
code: 'ERR_INVALID_ASYNC_ID',
3332
type: RangeError,
34-
}));
33+
});
3534

36-
assert.throws(()=>{
35+
common.expectsError(()=>{
3736
newAsyncResource('type',Math.PI);
38-
},common.expectsError({
37+
},{
3938
code: 'ERR_INVALID_ASYNC_ID',
4039
type: RangeError,
41-
}));
40+
});

‎test/parallel/test-async-wrap-constructor.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
// This tests that using falsy values in createHook throws an error.
44

55
constcommon=require('../common');
6-
constassert=require('assert');
76
constasync_hooks=require('async_hooks');
87

98
for(constbadArgof[0,1,false,true,null,'hello']){
109
consthookNames=['init','before','after','destroy','promiseResolve'];
1110
for(constfieldofhookNames){
12-
assert.throws(()=>{
11+
common.expectsError(()=>{
1312
async_hooks.createHook({[field]: badArg});
14-
},common.expectsError({
13+
},{
1514
code: 'ERR_ASYNC_CALLBACK',
1615
type: TypeError,
17-
}));
16+
});
1817
}
1918
}

‎test/parallel/test-buffer-alloc.js‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -887,12 +887,14 @@ assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError);
887887
}
888888

889889
// Regression test for #5482: should throw but not assert in C++ land.
890-
assert.throws(()=>Buffer.from('','buffer'),
891-
common.expectsError({
892-
code: 'ERR_UNKNOWN_ENCODING',
893-
type: TypeError,
894-
message: 'Unknown encoding: buffer'
895-
}));
890+
common.expectsError(
891+
()=>Buffer.from('','buffer'),
892+
{
893+
code: 'ERR_UNKNOWN_ENCODING',
894+
type: TypeError,
895+
message: 'Unknown encoding: buffer'
896+
}
897+
);
896898

897899
// Regression test for #6111. Constructing a buffer from another buffer
898900
// should a) work, and b) not corrupt the source buffer.

‎test/parallel/test-buffer-arraybuffer.js‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ b.writeDoubleBE(11.11, 0, true);
6565
buf[0]=9;
6666
assert.strictEqual(ab[1],9);
6767

68-
assert.throws(()=>Buffer.from(ab.buffer,6),common.expectsError({
68+
common.expectsError(()=>Buffer.from(ab.buffer,6),{
6969
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
7070
type: RangeError,
7171
message: '"offset" is outside of buffer bounds'
72-
}));
73-
assert.throws(()=>Buffer.from(ab.buffer,3,6),common.expectsError({
72+
});
73+
common.expectsError(()=>Buffer.from(ab.buffer,3,6),{
7474
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
7575
type: RangeError,
7676
message: '"length" is outside of buffer bounds'
77-
}));
77+
});
7878
}
7979

8080
// Test the deprecated Buffer() version also
@@ -93,16 +93,16 @@ b.writeDoubleBE(11.11, 0, true);
9393
buf[0]=9;
9494
assert.strictEqual(ab[1],9);
9595

96-
assert.throws(()=>Buffer(ab.buffer,6),common.expectsError({
96+
common.expectsError(()=>Buffer(ab.buffer,6),{
9797
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
9898
type: RangeError,
9999
message: '"offset" is outside of buffer bounds'
100-
}));
101-
assert.throws(()=>Buffer(ab.buffer,3,6),common.expectsError({
100+
});
101+
common.expectsError(()=>Buffer(ab.buffer,3,6),{
102102
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
103103
type: RangeError,
104104
message: '"length" is outside of buffer bounds'
105-
}));
105+
});
106106
}
107107

108108
{
@@ -118,13 +118,13 @@ b.writeDoubleBE(11.11, 0, true);
118118
assert.deepStrictEqual(Buffer.from(ab,[1]),Buffer.from(ab,1));
119119

120120
// If byteOffset is Infinity, throw.
121-
assert.throws(()=>{
121+
common.expectsError(()=>{
122122
Buffer.from(ab,Infinity);
123-
},common.expectsError({
123+
},{
124124
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
125125
type: RangeError,
126126
message: '"offset" is outside of buffer bounds'
127-
}));
127+
});
128128
}
129129

130130
{
@@ -140,11 +140,11 @@ b.writeDoubleBE(11.11, 0, true);
140140
assert.deepStrictEqual(Buffer.from(ab,0,[1]),Buffer.from(ab,0,1));
141141

142142
//If length is Infinity, throw.
143-
assert.throws(()=>{
143+
common.expectsError(()=>{
144144
Buffer.from(ab,0,Infinity);
145-
},common.expectsError({
145+
},{
146146
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
147147
type: RangeError,
148148
message: '"length" is outside of buffer bounds'
149-
}));
149+
});
150150
}

‎test/parallel/test-buffer-compare-offset.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ assert.throws(() => a.compare(b, 0, '0xff'), oor);
6666
assert.throws(()=>a.compare(b,0,Infinity),oor);
6767
assert.throws(()=>a.compare(b,0,1,-1),oor);
6868
assert.throws(()=>a.compare(b,-Infinity,Infinity),oor);
69-
assert.throws(()=>a.compare(),common.expectsError({
69+
common.expectsError(()=>a.compare(),{
7070
code: 'ERR_INVALID_ARG_TYPE',
7171
type: TypeError,
7272
message: 'The "target" argument must be one of '+
7373
'type Buffer or Uint8Array. Received type undefined'
74-
}));
74+
});

‎test/parallel/test-buffer-compare.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ assert.throws(() => Buffer.compare(Buffer.alloc(1), 'abc'), errMsg);
3838

3939
assert.throws(()=>Buffer.compare('abc',Buffer.alloc(1)),errMsg);
4040

41-
assert.throws(()=>Buffer.alloc(1).compare('abc'),common.expectsError({
41+
common.expectsError(()=>Buffer.alloc(1).compare('abc'),{
4242
code: 'ERR_INVALID_ARG_TYPE',
4343
type: TypeError,
4444
message: 'The "target" argument must be one of '+
4545
'type Buffer or Uint8Array. Received type string'
46-
}));
46+
});

‎test/parallel/test-buffer-concat.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ assertWrongList(['hello', 'world']);
5252
assertWrongList(['hello',Buffer.from('world')]);
5353

5454
functionassertWrongList(value){
55-
assert.throws(()=>{
55+
common.expectsError(()=>{
5656
Buffer.concat(value);
57-
},common.expectsError({
57+
},{
5858
code: 'ERR_INVALID_ARG_TYPE',
5959
type: TypeError,
6060
message: 'The "list" argument must be one of type '+
6161
'Array, Buffer, or Uint8Array'
62-
}));
62+
});
6363
}
6464

6565
// eslint-disable-next-line crypto-check

‎test/parallel/test-buffer-fill.js‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,12 @@ function testBufs(string, offset, length, encoding){
303303
}
304304

305305
// Make sure these throw.
306-
assert.throws(
306+
common.expectsError(
307307
()=>Buffer.allocUnsafe(8).fill('a',-1),
308-
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
309-
assert.throws(
308+
{code: 'ERR_INDEX_OUT_OF_RANGE'});
309+
common.expectsError(
310310
()=>Buffer.allocUnsafe(8).fill('a',0,9),
311-
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'}));
311+
{code: 'ERR_INDEX_OUT_OF_RANGE'});
312312

313313
// Make sure this doesn't hang indefinitely.
314314
Buffer.allocUnsafe(8).fill('');
@@ -357,7 +357,7 @@ Buffer.alloc(8, '');
357357
// magically mangled using Symbol.toPrimitive.
358358
{
359359
letelseWasLast=false;
360-
assert.throws(()=>{
360+
common.expectsError(()=>{
361361
letctr=0;
362362
conststart={
363363
[Symbol.toPrimitive](){
@@ -374,25 +374,27 @@ Buffer.alloc(8, '');
374374
}
375375
};
376376
Buffer.alloc(1).fill(Buffer.alloc(1),start,1);
377-
},common.expectsError(
378-
{code: undefined,type: RangeError,message: 'Index out of range'}));
377+
},{
378+
code: undefined,type: RangeError,message: 'Index out of range'
379+
});
379380
// Make sure -1 is making it to Buffer::Fill().
380381
assert.ok(elseWasLast,
381382
'internal API changed, -1 no longer in correct location');
382383
}
383384

384385
// Testing process.binding. Make sure "start" is properly checked for -1 wrap
385386
// around.
386-
assert.throws(()=>{
387+
common.expectsError(()=>{
387388
process.binding('buffer').fill(Buffer.alloc(1),1,-1,0,1);
388-
},common.expectsError(
389-
{code: undefined,type: RangeError,message: 'Index out of range'}));
389+
},{
390+
code: undefined,type: RangeError,message: 'Index out of range'
391+
});
390392

391393
// Make sure "end" is properly checked, even if it's magically mangled using
392394
// Symbol.toPrimitive.
393395
{
394396
letelseWasLast=false;
395-
assert.throws(()=>{
397+
common.expectsError(()=>{
396398
letctr=0;
397399
constend={
398400
[Symbol.toPrimitive](){
@@ -409,8 +411,9 @@ assert.throws(() =>{
409411
}
410412
};
411413
Buffer.alloc(1).fill(Buffer.alloc(1),0,end);
412-
},common.expectsError(
413-
{code: undefined,type: RangeError,message: 'Index out of range'}));
414+
},{
415+
code: undefined,type: RangeError,message: 'Index out of range'
416+
});
414417
// Make sure -1 is making it to Buffer::Fill().
415418
assert.ok(elseWasLast,
416419
'internal API changed, -1 no longer in correct location');

‎test/parallel/test-buffer-new.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict';
22

33
constcommon=require('../common');
4-
constassert=require('assert');
54

6-
assert.throws(()=>newBuffer(42,'utf8'),common.expectsError({
5+
common.expectsError(()=>newBuffer(42,'utf8'),{
76
code: 'ERR_INVALID_ARG_TYPE',
87
type: TypeError,
98
message: 'The "string" argument must be of type string. Received type number'
10-
}));
9+
});

‎test/parallel/test-buffer-read.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ const buf = Buffer.from([0xa4, 0xfd, 0x48, 0xea, 0xcf, 0xff, 0xd9, 0x01, 0xde]);
88
functionread(buff,funx,args,expected){
99

1010
assert.strictEqual(buff[funx](...args),expected);
11-
assert.throws(
11+
common.expectsError(
1212
()=>buff[funx](-1),
13-
common.expectsError({code: 'ERR_INDEX_OUT_OF_RANGE'})
13+
{
14+
code: 'ERR_INDEX_OUT_OF_RANGE'
15+
}
1416
);
1517

1618
assert.doesNotThrow(

0 commit comments

Comments
(0)