Skip to content

Commit 75bc40f

Browse files
committed
test: ensure assertions are reached on more tests
PR-URL: #60485 Reviewed-By: Colin Ihrig <[email protected]>
1 parent 1a6084c commit 75bc40f

30 files changed

+258
-296
lines changed

‎test/eslint.config_partial.mjs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ export default [
192192
`test/parallel/test-{${
193193
// 0x61 is code for 'a', this generates a string enumerating latin letters: 'z*,y*,…'
194194
Array.from({length: 2},(_,i)=>String.fromCharCode(0x61+25-i,42)).join(',')
195+
},${
196+
// 0x61 is code for 'a', this generates a string enumerating latin letters: 'a*,b*,…'
197+
Array.from({length: 2},(_,i)=>String.fromCharCode(0x61+i,42)).join(',')
195198
}}.{js,mjs,cjs}`,
196199
],
197200
rules: {

‎test/parallel/test-abortcontroller-internal.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
'use strict';
33
require('../common');
44

5-
const{
6-
strictEqual,
7-
}=require('assert');
5+
constassert=require('assert');
86

97
const{
108
test,
@@ -30,5 +28,5 @@ test('A weak event listener should not prevent gc', async () =>{
3028

3129
awaitsleep(10);
3230
globalThis.gc();
33-
strictEqual(ref.deref(),undefined);
31+
assert.strictEqual(ref.deref(),undefined);
3432
});

‎test/parallel/test-abortcontroller.js‎

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
// Flags: --expose-gc
22
'use strict';
33

4-
require('../common');
4+
constcommon=require('../common');
55
const{ inspect }=require('util');
66

7-
const{
8-
ok,
9-
notStrictEqual,
10-
strictEqual,
11-
throws,
12-
}=require('assert');
7+
constassert=require('assert');
138

149
const{
1510
test,
@@ -25,34 +20,34 @@ const{setTimeout: sleep } = require('timers/promises');
2520
test('Abort is fired with the correct event type on AbortControllers',()=>{
2621
// Tests that abort is fired with the correct event type on AbortControllers
2722
constac=newAbortController();
28-
ok(ac.signal);
23+
assert.ok(ac.signal);
2924

30-
constfn=mock.fn((event)=>{
31-
ok(event);
32-
strictEqual(event.type,'abort');
33-
});
25+
constfn=mock.fn(common.mustCall((event)=>{
26+
assert.ok(event);
27+
assert.strictEqual(event.type,'abort');
28+
},2));
3429

3530
ac.signal.onabort=fn;
3631
ac.signal.addEventListener('abort',fn);
3732

3833
ac.abort();
3934
ac.abort();
40-
ok(ac.signal.aborted);
35+
assert.ok(ac.signal.aborted);
4136

42-
strictEqual(fn.mock.calls.length,2);
37+
assert.strictEqual(fn.mock.calls.length,2);
4338
});
4439

4540
test('Abort events are trusted',()=>{
4641
// Tests that abort events are trusted
4742
constac=newAbortController();
4843

49-
constfn=mock.fn((event)=>{
50-
ok(event.isTrusted);
51-
});
44+
constfn=mock.fn(common.mustCall((event)=>{
45+
assert.ok(event.isTrusted);
46+
}));
5247

5348
ac.signal.onabort=fn;
5449
ac.abort();
55-
strictEqual(fn.mock.calls.length,1);
50+
assert.strictEqual(fn.mock.calls.length,1);
5651
});
5752

5853
test('Abort events have the same isTrusted reference',()=>{
@@ -73,14 +68,14 @@ test('Abort events have the same isTrusted reference', () =>{
7368
constfirstTrusted=Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev1),'isTrusted').get;
7469
constsecondTrusted=Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev2),'isTrusted').get;
7570
constuntrusted=Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev3),'isTrusted').get;
76-
strictEqual(firstTrusted,secondTrusted);
77-
strictEqual(untrusted,firstTrusted);
71+
assert.strictEqual(firstTrusted,secondTrusted);
72+
assert.strictEqual(untrusted,firstTrusted);
7873
});
7974

8075
test('AbortSignal is impossible to construct manually',()=>{
8176
// Tests that AbortSignal is impossible to construct manually
8277
constac=newAbortController();
83-
throws(()=>newac.signal.constructor(),{
78+
assert.throws(()=>newac.signal.constructor(),{
8479
code: 'ERR_ILLEGAL_CONSTRUCTOR',
8580
});
8681
});
@@ -89,13 +84,13 @@ test('Symbol.toStringTag is correct', () =>{
8984
// Symbol.toStringTag
9085
consttoString=(o)=>Object.prototype.toString.call(o);
9186
constac=newAbortController();
92-
strictEqual(toString(ac),'[object AbortController]');
93-
strictEqual(toString(ac.signal),'[object AbortSignal]');
87+
assert.strictEqual(toString(ac),'[object AbortController]');
88+
assert.strictEqual(toString(ac.signal),'[object AbortSignal]');
9489
});
9590

9691
test('AbortSignal.abort() creates an already aborted signal',()=>{
9792
constsignal=AbortSignal.abort();
98-
ok(signal.aborted);
93+
assert.ok(signal.aborted);
9994
});
10095

10196
test('AbortController properties and methods valiate the receiver',()=>{
@@ -106,7 +101,7 @@ test('AbortController properties and methods valiate the receiver', () =>{
106101
constacAbort=AbortController.prototype.abort;
107102

108103
constgoodController=newAbortController();
109-
ok(acSignalGet.call(goodController));
104+
assert.ok(acSignalGet.call(goodController));
110105
acAbort.call(goodController);
111106

112107
constbadAbortControllers=[
@@ -119,11 +114,11 @@ test('AbortController properties and methods valiate the receiver', () =>{
119114
{__proto__: AbortController.prototype},
120115
];
121116
for(constbadControllerofbadAbortControllers){
122-
throws(
117+
assert.throws(
123118
()=>acSignalGet.call(badController),
124119
{name: 'TypeError'}
125120
);
126-
throws(
121+
assert.throws(
127122
()=>acAbort.call(badController),
128123
{name: 'TypeError'}
129124
);
@@ -137,7 +132,7 @@ test('AbortSignal properties validate the receiver', () =>{
137132
).get;
138133

139134
constgoodSignal=newAbortController().signal;
140-
strictEqual(signalAbortedGet.call(goodSignal),false);
135+
assert.strictEqual(signalAbortedGet.call(goodSignal),false);
141136

142137
constbadAbortSignals=[
143138
null,
@@ -149,7 +144,7 @@ test('AbortSignal properties validate the receiver', () =>{
149144
{__proto__: AbortSignal.prototype},
150145
];
151146
for(constbadSignalofbadAbortSignals){
152-
throws(
147+
assert.throws(
153148
()=>signalAbortedGet.call(badSignal),
154149
{name: 'TypeError'}
155150
);
@@ -158,38 +153,38 @@ test('AbortSignal properties validate the receiver', () =>{
158153

159154
test('AbortController inspection depth 1 or null works',()=>{
160155
constac=newAbortController();
161-
strictEqual(inspect(ac,{depth: 1}),
162-
'AbortController{signal: [AbortSignal] }');
163-
strictEqual(inspect(ac,{depth: null}),
164-
'AbortController{signal: AbortSignal{aborted: false } }');
156+
assert.strictEqual(inspect(ac,{depth: 1}),
157+
'AbortController{signal: [AbortSignal] }');
158+
assert.strictEqual(inspect(ac,{depth: null}),
159+
'AbortController{signal: AbortSignal{aborted: false } }');
165160
});
166161

167162
test('AbortSignal reason is set correctly',()=>{
168163
// Test AbortSignal.reason
169164
constac=newAbortController();
170165
ac.abort('reason');
171-
strictEqual(ac.signal.reason,'reason');
166+
assert.strictEqual(ac.signal.reason,'reason');
172167
});
173168

174169
test('AbortSignal reasonable is set correctly with AbortSignal.abort()',()=>{
175170
// Test AbortSignal.reason
176171
constsignal=AbortSignal.abort('reason');
177-
strictEqual(signal.reason,'reason');
172+
assert.strictEqual(signal.reason,'reason');
178173
});
179174

180175
test('AbortSignal.timeout() works as expected',async()=>{
181176
// Test AbortSignal timeout
182177
constsignal=AbortSignal.timeout(10);
183-
ok(!signal.aborted);
178+
assert.ok(!signal.aborted);
184179

185180
const{ promise, resolve }=Promise.withResolvers();
186181

187-
constfn=mock.fn(()=>{
188-
ok(signal.aborted);
189-
strictEqual(signal.reason.name,'TimeoutError');
190-
strictEqual(signal.reason.code,23);
182+
constfn=mock.fn(common.mustCall(()=>{
183+
assert.ok(signal.aborted);
184+
assert.strictEqual(signal.reason.name,'TimeoutError');
185+
assert.strictEqual(signal.reason.code,23);
191186
resolve();
192-
});
187+
}));
193188

194189
setTimeout(fn,20);
195190
awaitpromise;
@@ -205,7 +200,7 @@ test('AbortSignal.timeout() does not prevent the signal from being collected', a
205200

206201
awaitsleep(10);
207202
globalThis.gc();
208-
strictEqual(ref.deref(),undefined);
203+
assert.strictEqual(ref.deref(),undefined);
209204
});
210205

211206
test('AbortSignal with a timeout is not collected while there is an active listener',async()=>{
@@ -220,14 +215,14 @@ test('AbortSignal with a timeout is not collected while there is an active liste
220215

221216
awaitsleep(10);
222217
globalThis.gc();
223-
notStrictEqual(ref.deref(),undefined);
224-
ok(ref.deref()instanceofAbortSignal);
218+
assert.notStrictEqual(ref.deref(),undefined);
219+
assert.ok(ref.deref()instanceofAbortSignal);
225220

226221
ref.deref().removeEventListener('abort',handler);
227222

228223
awaitsleep(10);
229224
globalThis.gc();
230-
strictEqual(ref.deref(),undefined);
225+
assert.strictEqual(ref.deref(),undefined);
231226
});
232227

233228
test('Setting a long timeout should not keep the process open',()=>{
@@ -237,18 +232,18 @@ test('Setting a long timeout should not keep the process open', () =>{
237232
test('AbortSignal.reason should default',()=>{
238233
// Test AbortSignal.reason default
239234
constsignal=AbortSignal.abort();
240-
ok(signal.reasoninstanceofDOMException);
241-
strictEqual(signal.reason.code,20);
235+
assert.ok(signal.reasoninstanceofDOMException);
236+
assert.strictEqual(signal.reason.code,20);
242237

243238
constac=newAbortController();
244239
ac.abort();
245-
ok(ac.signal.reasoninstanceofDOMException);
246-
strictEqual(ac.signal.reason.code,20);
240+
assert.ok(ac.signal.reasoninstanceofDOMException);
241+
assert.strictEqual(ac.signal.reason.code,20);
247242
});
248243

249244
test('abortSignal.throwIfAborted() works as expected',()=>{
250245
// Test abortSignal.throwIfAborted()
251-
throws(()=>AbortSignal.abort().throwIfAborted(),{
246+
assert.throws(()=>AbortSignal.abort().throwIfAborted(),{
252247
code: 20,
253248
name: 'AbortError',
254249
});
@@ -262,7 +257,7 @@ test('abortSignal.throwIfAobrted() works as expected (2)', () =>{
262257
constoriginalDesc=Reflect.getOwnPropertyDescriptor(AbortSignal.prototype,'aborted');
263258
constactualReason=newError();
264259
Reflect.defineProperty(AbortSignal.prototype,'aborted',{value: false});
265-
throws(()=>AbortSignal.abort(actualReason).throwIfAborted(),actualReason);
260+
assert.throws(()=>AbortSignal.abort(actualReason).throwIfAborted(),actualReason);
266261
Reflect.defineProperty(AbortSignal.prototype,'aborted',originalDesc);
267262
});
268263

@@ -271,6 +266,6 @@ test('abortSignal.throwIfAobrted() works as expected (3)', () =>{
271266
constactualReason=newError();
272267
constfakeExcuse=newError();
273268
Reflect.defineProperty(AbortSignal.prototype,'reason',{value: fakeExcuse});
274-
throws(()=>AbortSignal.abort(actualReason).throwIfAborted(),actualReason);
269+
assert.throws(()=>AbortSignal.abort(actualReason).throwIfAborted(),actualReason);
275270
Reflect.defineProperty(AbortSignal.prototype,'reason',originalDesc);
276271
});

‎test/parallel/test-aborted-util.js‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
// Flags: --expose-gc
22
'use strict';
33

4-
require('../common');
4+
constcommon=require('../common');
55
const{ aborted }=require('util');
6-
const{
7-
match,
8-
rejects,
9-
strictEqual,
10-
}=require('assert');
6+
constassert=require('assert');
117
const{ getEventListeners }=require('events');
128
const{ inspect }=require('util');
139

@@ -20,8 +16,8 @@ test('Aborted works when provided a resource', async () =>{
2016
constpromise=aborted(ac.signal,{});
2117
ac.abort();
2218
awaitpromise;
23-
strictEqual(ac.signal.aborted,true);
24-
strictEqual(getEventListeners(ac.signal,'abort').length,0);
19+
assert.strictEqual(ac.signal.aborted,true);
20+
assert.strictEqual(getEventListeners(ac.signal,'abort').length,0);
2521
});
2622

2723
test('Aborted with gc cleanup',async()=>{
@@ -31,23 +27,23 @@ test('Aborted with gc cleanup', async () =>{
3127
constabortedPromise=aborted(ac.signal,{});
3228
const{ promise, resolve }=Promise.withResolvers();
3329

34-
setImmediate(()=>{
30+
setImmediate(common.mustCall(()=>{
3531
globalThis.gc();
3632
ac.abort();
37-
strictEqual(ac.signal.aborted,true);
38-
strictEqual(getEventListeners(ac.signal,'abort').length,0);
33+
assert.strictEqual(ac.signal.aborted,true);
34+
assert.strictEqual(getEventListeners(ac.signal,'abort').length,0);
3935
resolve();
40-
});
36+
}));
4137

4238
awaitpromise;
4339

4440
// Ensure that the promise is still pending
45-
match(inspect(abortedPromise),/<pending>/);
41+
assert.match(inspect(abortedPromise),/<pending>/);
4642
});
4743

4844
test('Fails with error if not provided AbortSignal',async()=>{
4945
awaitPromise.all([{},null,undefined,Symbol(),[],1,0,1n,true,false,'a',()=>{}].map((sig)=>
50-
rejects(aborted(sig,{}),{
46+
assert.rejects(aborted(sig,{}),{
5147
code: 'ERR_INVALID_ARG_TYPE',
5248
})
5349
));
@@ -57,7 +53,7 @@ test('Fails if not provided a resource', async () =>{
5753
// Fails if not provided a resource
5854
constac=newAbortController();
5955
awaitPromise.all([null,undefined,0,1,0n,1n,Symbol(),'','a'].map((resource)=>
60-
rejects(aborted(ac.signal,resource),{
56+
assert.rejects(aborted(ac.signal,resource),{
6157
code: 'ERR_INVALID_ARG_TYPE',
6258
})
6359
));
@@ -82,10 +78,10 @@ function lazySpawn(){
8278
test('Does not hang forever',{skip: !lazySpawn()},async()=>{
8379
const{ promise, resolve }=Promise.withResolvers();
8480
constchildProcess=spawn(process.execPath,['--input-type=module']);
85-
childProcess.on('exit',(code)=>{
86-
strictEqual(code,13);
81+
childProcess.on('exit',common.mustCall((code)=>{
82+
assert.strictEqual(code,13);
8783
resolve();
88-
});
84+
}));
8985
childProcess.stdin.end(`
9086
import{aborted } from 'node:util'
9187
await aborted(new AbortController().signal,{});

0 commit comments

Comments
(0)