test: ensure assertions are reached on more tests · nodejs/node@6aaf18c

11

// Flags: --expose-gc

22

'use strict';

334-

require('../common');

4+

const common = require('../common');

55

const { inspect } = require('util');

667-

const {

8-

ok,

9-

notStrictEqual,

10-

strictEqual,

11-

throws,

12-

} = require('assert');

7+

const assert = require('assert');

138149

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

const ac = new AbortController();

28-

ok(ac.signal);

23+

assert.ok(ac.signal);

292430-

const fn = mock.fn((event) => {

31-

ok(event);

32-

strictEqual(event.type, 'abort');

33-

});

25+

const fn = mock.fn(common.mustCall((event) => {

26+

assert.ok(event);

27+

assert.strictEqual(event.type, 'abort');

28+

}, 2));

34293530

ac.signal.onabort = fn;

3631

ac.signal.addEventListener('abort', fn);

37323833

ac.abort();

3934

ac.abort();

40-

ok(ac.signal.aborted);

35+

assert.ok(ac.signal.aborted);

413642-

strictEqual(fn.mock.calls.length, 2);

37+

assert.strictEqual(fn.mock.calls.length, 2);

4338

});

44394540

test('Abort events are trusted', () => {

4641

// Tests that abort events are trusted

4742

const ac = new AbortController();

484349-

const fn = mock.fn((event) => {

50-

ok(event.isTrusted);

51-

});

44+

const fn = mock.fn(common.mustCall((event) => {

45+

assert.ok(event.isTrusted);

46+

}));

52475348

ac.signal.onabort = fn;

5449

ac.abort();

55-

strictEqual(fn.mock.calls.length, 1);

50+

assert.strictEqual(fn.mock.calls.length, 1);

5651

});

57525853

test('Abort events have the same isTrusted reference', () => {

@@ -73,14 +68,14 @@ test('Abort events have the same isTrusted reference', () => {

7368

const firstTrusted = Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev1), 'isTrusted').get;

7469

const secondTrusted = Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev2), 'isTrusted').get;

7570

const untrusted = 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

});

79748075

test('AbortSignal is impossible to construct manually', () => {

8176

// Tests that AbortSignal is impossible to construct manually

8277

const ac = new AbortController();

83-

throws(() => new ac.signal.constructor(), {

78+

assert.throws(() => new ac.signal.constructor(), {

8479

code: 'ERR_ILLEGAL_CONSTRUCTOR',

8580

});

8681

});

@@ -89,13 +84,13 @@ test('Symbol.toStringTag is correct', () => {

8984

// Symbol.toStringTag

9085

const toString = (o) => Object.prototype.toString.call(o);

9186

const ac = new AbortController();

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

});

95909691

test('AbortSignal.abort() creates an already aborted signal', () => {

9792

const signal = AbortSignal.abort();

98-

ok(signal.aborted);

93+

assert.ok(signal.aborted);

9994

});

1009510196

test('AbortController properties and methods valiate the receiver', () => {

@@ -106,7 +101,7 @@ test('AbortController properties and methods valiate the receiver', () => {

106101

const acAbort = AbortController.prototype.abort;

107102108103

const goodController = new AbortController();

109-

ok(acSignalGet.call(goodController));

104+

assert.ok(acSignalGet.call(goodController));

110105

acAbort.call(goodController);

111106112107

const badAbortControllers = [

@@ -119,11 +114,11 @@ test('AbortController properties and methods valiate the receiver', () => {

119114

{ __proto__: AbortController.prototype },

120115

];

121116

for (const badController of badAbortControllers) {

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;

138133139134

const goodSignal = new AbortController().signal;

140-

strictEqual(signalAbortedGet.call(goodSignal), false);

135+

assert.strictEqual(signalAbortedGet.call(goodSignal), false);

141136142137

const badAbortSignals = [

143138

null,

@@ -149,7 +144,7 @@ test('AbortSignal properties validate the receiver', () => {

149144

{ __proto__: AbortSignal.prototype },

150145

];

151146

for (const badSignal of badAbortSignals) {

152-

throws(

147+

assert.throws(

153148

() => signalAbortedGet.call(badSignal),

154149

{ name: 'TypeError' }

155150

);

@@ -158,38 +153,38 @@ test('AbortSignal properties validate the receiver', () => {

158153159154

test('AbortController inspection depth 1 or null works', () => {

160155

const ac = new AbortController();

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

});

166161167162

test('AbortSignal reason is set correctly', () => {

168163

// Test AbortSignal.reason

169164

const ac = new AbortController();

170165

ac.abort('reason');

171-

strictEqual(ac.signal.reason, 'reason');

166+

assert.strictEqual(ac.signal.reason, 'reason');

172167

});

173168174169

test('AbortSignal reasonable is set correctly with AbortSignal.abort()', () => {

175170

// Test AbortSignal.reason

176171

const signal = AbortSignal.abort('reason');

177-

strictEqual(signal.reason, 'reason');

172+

assert.strictEqual(signal.reason, 'reason');

178173

});

179174180175

test('AbortSignal.timeout() works as expected', async () => {

181176

// Test AbortSignal timeout

182177

const signal = AbortSignal.timeout(10);

183-

ok(!signal.aborted);

178+

assert.ok(!signal.aborted);

184179185180

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

186181187-

const fn = mock.fn(() => {

188-

ok(signal.aborted);

189-

strictEqual(signal.reason.name, 'TimeoutError');

190-

strictEqual(signal.reason.code, 23);

182+

const fn = 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+

}));

193188194189

setTimeout(fn, 20);

195190

await promise;

@@ -205,7 +200,7 @@ test('AbortSignal.timeout() does not prevent the signal from being collected', a

205200206201

await sleep(10);

207202

globalThis.gc();

208-

strictEqual(ref.deref(), undefined);

203+

assert.strictEqual(ref.deref(), undefined);

209204

});

210205211206

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

220215221216

await sleep(10);

222217

globalThis.gc();

223-

notStrictEqual(ref.deref(), undefined);

224-

ok(ref.deref() instanceof AbortSignal);

218+

assert.notStrictEqual(ref.deref(), undefined);

219+

assert.ok(ref.deref() instanceof AbortSignal);

225220226221

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

227222228223

await sleep(10);

229224

globalThis.gc();

230-

strictEqual(ref.deref(), undefined);

225+

assert.strictEqual(ref.deref(), undefined);

231226

});

232227233228

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

const signal = AbortSignal.abort();

240-

ok(signal.reason instanceof DOMException);

241-

strictEqual(signal.reason.code, 20);

235+

assert.ok(signal.reason instanceof DOMException);

236+

assert.strictEqual(signal.reason.code, 20);

242237243238

const ac = new AbortController();

244239

ac.abort();

245-

ok(ac.signal.reason instanceof DOMException);

246-

strictEqual(ac.signal.reason.code, 20);

240+

assert.ok(ac.signal.reason instanceof DOMException);

241+

assert.strictEqual(ac.signal.reason.code, 20);

247242

});

248243249244

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

const originalDesc = Reflect.getOwnPropertyDescriptor(AbortSignal.prototype, 'aborted');

263258

const actualReason = new Error();

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

const actualReason = new Error();

272267

const fakeExcuse = new Error();

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

});