Skip to content

Commit 0015430

Browse files
BridgeARtargos
authored andcommitted
assert: align argument names
This makes sure the documented argument names and the ones thrown in errors is aligned with the actual argument name. PR-URL: #22760 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
1 parent b1d667b commit 0015430

6 files changed

Lines changed: 72 additions & 80 deletions

File tree

‎lib/assert.js‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,12 @@ function expectedException(actual, expected, msg) {
557557
returnexpected.call({},actual)===true;
558558
}
559559

560-
functiongetActual(block){
561-
if(typeofblock!=='function'){
562-
thrownewERR_INVALID_ARG_TYPE('block','Function',block);
560+
functiongetActual(fn){
561+
if(typeoffn!=='function'){
562+
thrownewERR_INVALID_ARG_TYPE('fn','Function',fn);
563563
}
564564
try{
565-
block();
565+
fn();
566566
}catch(e){
567567
returne;
568568
}
@@ -579,20 +579,21 @@ function checkIsPromise(obj) {
579579
typeofobj.catch==='function';
580580
}
581581

582-
asyncfunctionwaitForActual(block){
582+
asyncfunctionwaitForActual(promiseFn){
583583
letresultPromise;
584-
if(typeofblock==='function'){
585-
// Return a rejected promise if `block` throws synchronously.
586-
resultPromise=block();
584+
if(typeofpromiseFn==='function'){
585+
// Return a rejected promise if `promiseFn` throws synchronously.
586+
resultPromise=promiseFn();
587587
// Fail in case no promise is returned.
588588
if(!checkIsPromise(resultPromise)){
589589
thrownewERR_INVALID_RETURN_VALUE('instance of Promise',
590-
'block',resultPromise);
590+
'promiseFn',resultPromise);
591591
}
592-
}elseif(checkIsPromise(block)){
593-
resultPromise=block;
592+
}elseif(checkIsPromise(promiseFn)){
593+
resultPromise=promiseFn;
594594
}else{
595-
thrownewERR_INVALID_ARG_TYPE('block',['Function','Promise'],block);
595+
thrownewERR_INVALID_ARG_TYPE(
596+
'promiseFn',['Function','Promise'],promiseFn);
596597
}
597598

598599
try{
@@ -672,20 +673,20 @@ function expectsNoError(stackStartFn, actual, error, message) {
672673
throwactual;
673674
}
674675

675-
assert.throws=functionthrows(block, ...args){
676-
expectsError(throws,getActual(block), ...args);
676+
assert.throws=functionthrows(promiseFn, ...args){
677+
expectsError(throws,getActual(promiseFn), ...args);
677678
};
678679

679-
assert.rejects=asyncfunctionrejects(block, ...args){
680-
expectsError(rejects,awaitwaitForActual(block), ...args);
680+
assert.rejects=asyncfunctionrejects(promiseFn, ...args){
681+
expectsError(rejects,awaitwaitForActual(promiseFn), ...args);
681682
};
682683

683-
assert.doesNotThrow=functiondoesNotThrow(block, ...args){
684-
expectsNoError(doesNotThrow,getActual(block), ...args);
684+
assert.doesNotThrow=functiondoesNotThrow(fn, ...args){
685+
expectsNoError(doesNotThrow,getActual(fn), ...args);
685686
};
686687

687-
assert.doesNotReject=asyncfunctiondoesNotReject(block, ...args){
688-
expectsNoError(doesNotReject,awaitwaitForActual(block), ...args);
688+
assert.doesNotReject=asyncfunctiondoesNotReject(fn, ...args){
689+
expectsNoError(doesNotReject,awaitwaitForActual(fn), ...args);
689690
};
690691

691692
assert.ifError=functionifError(err){

‎test/parallel/test-assert-async.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const promises = [];
4141
name: 'TypeError [ERR_INVALID_RETURN_VALUE]',
4242
code: 'ERR_INVALID_RETURN_VALUE',
4343
message: 'Expected instance of Promise to be returned '+
44-
'from the "block" function but got type undefined.'
44+
'from the "promiseFn" function but got type undefined.'
4545
}));
4646

4747
promise=assert.rejects(Promise.resolve(),common.mustNotCall());
@@ -62,7 +62,7 @@ promises.push(assert.rejects(
6262
assert.rejects('fail',{}),
6363
{
6464
code: 'ERR_INVALID_ARG_TYPE',
65-
message: 'The "block" argument must be one of type '+
65+
message: 'The "promiseFn" argument must be one of type '+
6666
'Function or Promise. Received type string'
6767
}
6868
));
@@ -73,7 +73,7 @@ promises.push(assert.rejects(
7373
constpromise=assert.doesNotReject(()=>newMap(),common.mustNotCall());
7474
promises.push(assert.rejects(promise,{
7575
message: 'Expected instance of Promise to be returned '+
76-
'from the "block" function but got instance of Map.',
76+
'from the "promiseFn" function but got instance of Map.',
7777
code: 'ERR_INVALID_RETURN_VALUE',
7878
name: 'TypeError [ERR_INVALID_RETURN_VALUE]'
7979
}));
@@ -116,7 +116,7 @@ promises.push(assert.rejects(
116116
assert.doesNotReject(123),
117117
{
118118
code: 'ERR_INVALID_ARG_TYPE',
119-
message: 'The "block" argument must be one of type '+
119+
message: 'The "promiseFn" argument must be one of type '+
120120
'Function or Promise. Received type number'
121121
}
122122
));

‎test/parallel/test-assert.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,15 @@ try {
341341
}
342342

343343
{
344-
// Verify that throws() and doesNotThrow() throw on non-function block.
345-
consttestBlockTypeError=(method,block)=>{
344+
// Verify that throws() and doesNotThrow() throw on non-functions.
345+
consttestBlockTypeError=(method,fn)=>{
346346
common.expectsError(
347-
()=>method(block),
347+
()=>method(fn),
348348
{
349349
code: 'ERR_INVALID_ARG_TYPE',
350350
type: TypeError,
351-
message: 'The "block" argument must be of type Function. Received '+
352-
`type ${typeofblock}`
351+
message: 'The "fn" argument must be of type Function. Received '+
352+
`type ${typeoffn}`
353353
}
354354
);
355355
};

‎test/parallel/test-file-write-stream3.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ function run_test_3() {
178178

179179
construn_test_4=common.mustCall(function(){
180180
// Error: start must be >= zero
181-
constblock=()=>{
181+
constfn=()=>{
182182
fs.createWriteStream(filepath,{start: -5,flags: 'r+'});
183183
};
184184
consterr={
@@ -187,7 +187,7 @@ const run_test_4 = common.mustCall(function() {
187187
'It must be >= 0. Received {start: -5}',
188188
type: RangeError
189189
};
190-
common.expectsError(block,err);
190+
common.expectsError(fn,err);
191191
});
192192

193193
run_test_1();

‎test/parallel/test-net-connect-options-port.js‎

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ const net = require('net');
6262
consthints=(dns.ADDRCONFIG|dns.V4MAPPED)+42;
6363
consthintOptBlocks=doConnect([{ hints }],
6464
()=>common.mustNotCall());
65-
for(constblockofhintOptBlocks){
66-
common.expectsError(block,{
65+
for(constfnofhintOptBlocks){
66+
common.expectsError(fn,{
6767
code: 'ERR_INVALID_OPT_VALUE',
6868
type: TypeError,
6969
message: /Thevalue"\d+"isinvalidforoption"hints"/
@@ -136,67 +136,59 @@ function doConnect(args, getCb) {
136136
functionsyncFailToConnect(port,assertErr,optOnly){
137137
if(!optOnly){
138138
// connect(port, cb) and connect(port)
139-
constportArgBlocks=doConnect([port],()=>common.mustNotCall());
140-
for(constblockofportArgBlocks){
141-
assert.throws(block,
142-
assertErr,
143-
`${block.name}(${port})`);
139+
constportArgFunctions=doConnect([port],()=>common.mustNotCall());
140+
for(constfnofportArgFunctions){
141+
assert.throws(fn,assertErr,`${fn.name}(${port})`);
144142
}
145143

146144
// connect(port, host, cb) and connect(port, host)
147-
constportHostArgBlocks=doConnect([port,'localhost'],
148-
()=>common.mustNotCall());
149-
for(constblockofportHostArgBlocks){
150-
assert.throws(block,
151-
assertErr,
152-
`${block.name}(${port}, 'localhost')`);
145+
constportHostArgFunctions=doConnect([port,'localhost'],
146+
()=>common.mustNotCall());
147+
for(constfnofportHostArgFunctions){
148+
assert.throws(fn,assertErr,`${fn.name}(${port}, 'localhost')`);
153149
}
154150
}
155151
// connect({port}, cb) and connect({port})
156-
constportOptBlocks=doConnect([{ port }],
157-
()=>common.mustNotCall());
158-
for(constblockofportOptBlocks){
159-
assert.throws(block,
160-
assertErr,
161-
`${block.name}({port: ${port}})`);
152+
constportOptFunctions=doConnect([{ port }],()=>common.mustNotCall());
153+
for(constfnofportOptFunctions){
154+
assert.throws(fn,assertErr,`${fn.name}({port: ${port}})`);
162155
}
163156

164157
// connect({port, host}, cb) and connect({port, host})
165-
constportHostOptBlocks=doConnect([{port: port,host: 'localhost'}],
166-
()=>common.mustNotCall());
167-
for(constblockofportHostOptBlocks){
168-
assert.throws(block,
158+
constportHostOptFunctions=doConnect([{port: port,host: 'localhost'}],
159+
()=>common.mustNotCall());
160+
for(constfnofportHostOptFunctions){
161+
assert.throws(fn,
169162
assertErr,
170-
`${block.name}({port: ${port}, host: 'localhost'})`);
163+
`${fn.name}({port: ${port}, host: 'localhost'})`);
171164
}
172165
}
173166

174167
functioncanConnect(port){
175168
constnoop=()=>common.mustCall();
176169

177170
// connect(port, cb) and connect(port)
178-
constportArgBlocks=doConnect([port],noop);
179-
for(constblockofportArgBlocks){
180-
block();
171+
constportArgFunctions=doConnect([port],noop);
172+
for(constfnofportArgFunctions){
173+
fn();
181174
}
182175

183176
// connect(port, host, cb) and connect(port, host)
184-
constportHostArgBlocks=doConnect([port,'localhost'],noop);
185-
for(constblockofportHostArgBlocks){
186-
block();
177+
constportHostArgFunctions=doConnect([port,'localhost'],noop);
178+
for(constfnofportHostArgFunctions){
179+
fn();
187180
}
188181

189182
// connect({port}, cb) and connect({port})
190-
constportOptBlocks=doConnect([{ port }],noop);
191-
for(constblockofportOptBlocks){
192-
block();
183+
constportOptFunctions=doConnect([{ port }],noop);
184+
for(constfnofportOptFunctions){
185+
fn();
193186
}
194187

195188
// connect({port, host}, cb) and connect({port, host})
196-
constportHostOptBlocks=doConnect([{port: port,host: 'localhost'}],
197-
noop);
198-
for(constblockofportHostOptBlocks){
199-
block();
189+
constportHostOptFns=doConnect([{ port,host: 'localhost'}],noop);
190+
for(constfnofportHostOptFns){
191+
fn();
200192
}
201193
}
202194

@@ -208,21 +200,20 @@ function asyncFailToConnect(port) {
208200

209201
constdont=()=>common.mustNotCall();
210202
// connect(port, cb) and connect(port)
211-
constportArgBlocks=doConnect([port],dont);
212-
for(constblockofportArgBlocks){
213-
block().on('error',onError());
203+
constportArgFunctions=doConnect([port],dont);
204+
for(constfnofportArgFunctions){
205+
fn().on('error',onError());
214206
}
215207

216208
// connect({port}, cb) and connect({port})
217-
constportOptBlocks=doConnect([{ port }],dont);
218-
for(constblockofportOptBlocks){
219-
block().on('error',onError());
209+
constportOptFunctions=doConnect([{ port }],dont);
210+
for(constfnofportOptFunctions){
211+
fn().on('error',onError());
220212
}
221213

222214
// connect({port, host}, cb) and connect({port, host})
223-
constportHostOptBlocks=doConnect([{port: port,host: 'localhost'}],
224-
dont);
225-
for(constblockofportHostOptBlocks){
226-
block().on('error',onError());
215+
constportHostOptFns=doConnect([{ port,host: 'localhost'}],dont);
216+
for(constfnofportHostOptFns){
217+
fn().on('error',onError());
227218
}
228219
}

‎test/parallel/test-net-server-listen-options.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ const listenOnPort = [
5454

5555
{
5656
functionshouldFailToListen(options){
57-
constblock=()=>{
57+
constfn=()=>{
5858
net.createServer().listen(options,common.mustNotCall());
5959
};
60-
common.expectsError(block,
60+
common.expectsError(fn,
6161
{
6262
code: 'ERR_INVALID_OPT_VALUE',
6363
type: TypeError,

0 commit comments

Comments
 (0)