Skip to content

Commit 6a3d80f

Browse files
committed
test: ensure assertions are reached on all tests
Signed-off-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: #64716 Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 9e6f8a0 commit 6a3d80f

242 files changed

Lines changed: 1811 additions & 2383 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

test/common/fixtures.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fixtures from './fixtures.js';
22

3+
// eslint-disable-next-line no-restricted-syntax
34
const {
45
fixturesDir,
56
path,

test/eslint.config_partial.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ export default [
117117
selector: 'CallExpression:matches([callee.type="Identifier"][callee.name="assert"], [callee.type="MemberExpression"][callee.object.type="Identifier"][callee.object.name="assert"][callee.property.type="Identifier"][callee.property.name="ok"])[arguments.0.type="UnaryExpression"][arguments.0.operator="!"][arguments.0.argument.type="CallExpression"][arguments.0.argument.callee.type="MemberExpression"][arguments.0.argument.callee.object.regex][arguments.0.argument.callee.property.name="test"]',
118118
message: 'Use assert.doesNotMatch instead',
119119
},
120+
{
121+
selector: 'VariableDeclarator[init.type="Identifier"][init.name=/^(assert|fixtures)$/]',
122+
message: 'Do not destructure or rename `assert` nor `fixtures`',
123+
},
120124
...((fixturesSpecifier) => [
121125
{
122126
selector: `ImportDeclaration[source.value=${fixturesSpecifier.toString().replace('(\\.js)?', '\\.mjs')}]:not(${[

test/parallel/test-assert-deep.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const { mustCall, hasCrypto } = require('../common');
44
const assert = require('assert');
55
const util = require('util');
66
const { test } = require('node:test');
7-
const { AssertionError } = assert;
87
const defaultMsgStart = 'Expected values to be strictly deep-equal:\n';
98
const defaultMsgStartFull = `${defaultMsgStart}+ actual - expected`;
109

@@ -688,11 +687,11 @@ test('Handle sparse arrays', () => {
688687
const b = new Array(3);
689688
a[2] = true;
690689
b[1] = true;
691-
assertNotDeepOrStrict(a, b, AssertionError, { partial: 'pass' });
690+
assertNotDeepOrStrict(a, b, assert.AssertionError, { partial: 'pass' });
692691
b[2] = true;
693692
assertNotDeepOrStrict(a, b);
694693
a[0] = true;
695-
assertNotDeepOrStrict(a, b, AssertionError, { partial: 'pass' });
694+
assertNotDeepOrStrict(a, b, assert.AssertionError, { partial: 'pass' });
696695
});
697696

698697
test('Handle sets and maps with mixed keys', () => {
@@ -715,7 +714,7 @@ test('Handle different error messages', () => {
715714
assertNotDeepOrStrict(err1, new Error('foo2'), assert.AssertionError);
716715
assertNotDeepOrStrict(err1, new TypeError('foo1'), assert.AssertionError);
717716
assertDeepAndStrictEqual(err1, new Error('foo1'));
718-
assertNotDeepOrStrict(err1, {}, AssertionError);
717+
assertNotDeepOrStrict(err1, {}, assert.AssertionError);
719718
});
720719

721720
test('Handle NaN', () => {
@@ -811,18 +810,18 @@ test('Additional tests', () => {
811810
assertDeepAndStrictEqual(new Date(2000, 3, 14), new Date(2000, 3, 14));
812811

813812
assert.throws(() => { assert.deepEqual(new Date(), new Date(2000, 3, 14)); },
814-
AssertionError,
813+
assert.AssertionError,
815814
'deepEqual(new Date(), new Date(2000, 3, 14))');
816815

817816
assert.throws(
818817
() => { assert.notDeepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14)); },
819-
AssertionError,
818+
assert.AssertionError,
820819
'notDeepEqual(new Date(2000, 3, 14), new Date(2000, 3, 14))'
821820
);
822821

823822
assert.throws(
824823
() => { assert.notDeepEqual('a'.repeat(1024), 'a'.repeat(1024)); },
825-
AssertionError,
824+
assert.AssertionError,
826825
'notDeepEqual("a".repeat(1024), "a".repeat(1024))'
827826
);
828827

@@ -861,7 +860,7 @@ test('Additional tests', () => {
861860
assert.deepEqual(4, '4');
862861
assert.deepEqual(true, 1);
863862
assert.throws(() => assert.deepEqual(4, '5'),
864-
AssertionError,
863+
assert.AssertionError,
865864
'deepEqual( 4, \'5\')');
866865
});
867866

@@ -870,7 +869,7 @@ test('Having the same number of owned properties && the same set of keys', () =>
870869
assert.deepEqual({ a: 4, b: '2' }, { a: 4, b: '2' });
871870
assert.deepEqual([4], ['4']);
872871
assert.throws(
873-
() => assert.deepEqual({ a: 4 }, { a: 4, b: true }), AssertionError);
872+
() => assert.deepEqual({ a: 4 }, { a: 4, b: true }), assert.AssertionError);
874873
assert.notDeepEqual(['a'], { 0: 'a' });
875874
assert.deepEqual({ a: 4, b: '1' }, { b: '1', a: 4 });
876875
const a1 = [1, 2, 3];
@@ -880,7 +879,7 @@ test('Having the same number of owned properties && the same set of keys', () =>
880879
a2.b = true;
881880
a2.a = 'test';
882881
assert.throws(() => assert.deepEqual(Object.keys(a1), Object.keys(a2)),
883-
AssertionError);
882+
assert.AssertionError);
884883
assertDeepAndStrictEqual(a1, a2);
885884
});
886885

@@ -946,7 +945,7 @@ test('Additional tests', () => {
946945

947946
assert.throws(
948947
() => assert.deepStrictEqual(new Date(), new Date(2000, 3, 14)),
949-
AssertionError,
948+
assert.AssertionError,
950949
'deepStrictEqual(new Date(), new Date(2000, 3, 14))'
951950
);
952951

@@ -1059,7 +1058,7 @@ test('Additional tests', () => {
10591058

10601059
assert.throws(
10611060
() => assert.deepStrictEqual([0, 1, 2, 'a', 'b'], [0, 1, 2, 'b', 'a']),
1062-
AssertionError);
1061+
assert.AssertionError);
10631062
});
10641063

10651064
test('Having the same number of owned properties && the same set of keys', () => {
@@ -1105,7 +1104,7 @@ test('Prototype check', () => {
11051104
const obj1 = new Constructor1('Ryan', 'Dahl');
11061105
let obj2 = new Constructor2('Ryan', 'Dahl');
11071106

1108-
assert.throws(() => assert.deepStrictEqual(obj1, obj2), AssertionError);
1107+
assert.throws(() => assert.deepStrictEqual(obj1, obj2), assert.AssertionError);
11091108

11101109
Constructor2.prototype = Constructor1.prototype;
11111110
obj2 = new Constructor2('Ryan', 'Dahl');
@@ -1262,7 +1261,7 @@ test('Verify that changed tags will still check for the error message', () => {
12621261
err[Symbol.toStringTag] = 'Foobar';
12631262
const err2 = new Error('bar');
12641263
err2[Symbol.toStringTag] = 'Foobar';
1265-
assertNotDeepOrStrict(err, err2, AssertionError);
1264+
assertNotDeepOrStrict(err, err2, assert.AssertionError);
12661265
});
12671266

12681267
test('Check for non-native errors', () => {
@@ -1281,8 +1280,8 @@ test('Check for non-native errors', () => {
12811280
test('Check for Errors with cause property', () => {
12821281
const e1 = new Error('err', { cause: new Error('cause e1') });
12831282
const e2 = new Error('err', { cause: new Error('cause e2') });
1284-
assertNotDeepOrStrict(e1, e2, AssertionError);
1285-
assertNotDeepOrStrict(e1, new Error('err'), AssertionError);
1283+
assertNotDeepOrStrict(e1, e2, assert.AssertionError);
1284+
assertNotDeepOrStrict(e1, new Error('err'), assert.AssertionError);
12861285
assertDeepAndStrictEqual(e1, new Error('err', { cause: new Error('cause e1') }));
12871286
});
12881287

@@ -1294,8 +1293,8 @@ test('Check for AggregateError', () => {
12941293
const e3 = new AggregateError([e1duplicate, e2], 'Aggregate Error');
12951294
const e3duplicate = new AggregateError([e1, e2], 'Aggregate Error');
12961295
const e4 = new AggregateError([e1], 'Aggregate Error');
1297-
assertNotDeepOrStrict(e1, e3, AssertionError);
1298-
assertNotDeepOrStrict(e3, e4, AssertionError);
1296+
assertNotDeepOrStrict(e1, e3, assert.AssertionError);
1297+
assertNotDeepOrStrict(e3, e4, assert.AssertionError);
12991298
assertDeepAndStrictEqual(e3, e3duplicate);
13001299
});
13011300

test/parallel/test-dtls-alpn.mjs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
66
import assert from 'node:assert';
77
import * as fixtures from '../common/fixtures.mjs';
88

9-
const { strictEqual } = assert;
10-
const { readKey } = fixtures;
11-
129
if (!hasCrypto) {
1310
skip('missing crypto');
1411
}
@@ -19,17 +16,17 @@ if (!process.features.dtls) {
1916

2017
const { listen, connect } = await import('node:dtls');
2118

22-
const serverCert = readKey('agent1-cert.pem');
23-
const serverKey = readKey('agent1-key.pem');
24-
const ca = readKey('ca1-cert.pem');
19+
const serverCert = fixtures.readKey('agent1-cert.pem');
20+
const serverKey = fixtures.readKey('agent1-key.pem');
21+
const ca = fixtures.readKey('ca1-cert.pem');
2522

2623
const serverAlpnChecked = Promise.withResolvers();
2724

2825
const endpoint = listen(mustCall(async (session) => {
2926
session.onmessage = () => {};
3027
await session.opened;
3128
// Server should see the negotiated ALPN protocol.
32-
strictEqual(session.alpnProtocol, 'coap');
29+
assert.strictEqual(session.alpnProtocol, 'coap');
3330
serverAlpnChecked.resolve();
3431
}), {
3532
cert: serverCert.toString(),
@@ -48,7 +45,7 @@ const session = connect('127.0.0.1', endpoint.address.port, {
4845
await session.opened;
4946

5047
// Client should see the negotiated protocol.
51-
strictEqual(session.alpnProtocol, 'coap');
48+
assert.strictEqual(session.alpnProtocol, 'coap');
5249

5350
await serverAlpnChecked.promise;
5451

test/parallel/test-dtls-async-dispose.mjs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall, mustNotCall } from '../common/index.mjs';
66
import assert from 'node:assert';
77
import * as fixtures from '../common/fixtures.mjs';
88

9-
const { strictEqual } = assert;
10-
const { readKey } = fixtures;
11-
129
if (!hasCrypto) {
1310
skip('missing crypto');
1411
}
@@ -19,9 +16,9 @@ if (!process.features.dtls) {
1916

2017
const { listen, connect } = await import('node:dtls');
2118

22-
const serverCert = readKey('agent1-cert.pem');
23-
const serverKey = readKey('agent1-key.pem');
24-
const ca = readKey('ca1-cert.pem');
19+
const serverCert = fixtures.readKey('agent1-cert.pem');
20+
const serverKey = fixtures.readKey('agent1-key.pem');
21+
const ca = fixtures.readKey('ca1-cert.pem');
2522

2623
const endpoint = listen(mustCall((session) => {
2724
session.onmessage = mustNotCall();
@@ -40,8 +37,8 @@ const session = connect('127.0.0.1', endpoint.address.port, {
4037
await session.opened;
4138

4239
// Test that Symbol.asyncDispose exists.
43-
strictEqual(typeof session[Symbol.asyncDispose], 'function');
44-
strictEqual(typeof endpoint[Symbol.asyncDispose], 'function');
40+
assert.strictEqual(typeof session[Symbol.asyncDispose], 'function');
41+
assert.strictEqual(typeof endpoint[Symbol.asyncDispose], 'function');
4542

4643
// Dispose the session.
4744
await session[Symbol.asyncDispose]();

test/parallel/test-dtls-basic.mjs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
66
import assert from 'node:assert';
77
import * as fixtures from '../common/fixtures.mjs';
88

9-
const { ok, strictEqual, match } = assert;
10-
const { readKey } = fixtures;
11-
129
if (!hasCrypto) {
1310
skip('missing crypto');
1411
}
@@ -19,9 +16,9 @@ if (!process.features.dtls) {
1916

2017
const { listen, connect } = await import('node:dtls');
2118

22-
const serverCert = readKey('agent1-cert.pem');
23-
const serverKey = readKey('agent1-key.pem');
24-
const ca = readKey('ca1-cert.pem');
19+
const serverCert = fixtures.readKey('agent1-cert.pem');
20+
const serverKey = fixtures.readKey('agent1-key.pem');
21+
const ca = fixtures.readKey('ca1-cert.pem');
2522

2623
const serverReceivedData = Promise.withResolvers();
2724
const clientReceivedData = Promise.withResolvers();
@@ -32,16 +29,16 @@ let clientHandshakeDone = false;
3229
// Start server.
3330
const endpoint = listen(mustCall((session) => {
3431
session.onmessage = mustCall((data) => {
35-
strictEqual(data.toString(), 'hello from client');
32+
assert.strictEqual(data.toString(), 'hello from client');
3633
serverReceivedData.resolve();
3734

3835
// Send response back to client.
3936
session.send('hello from server');
4037
});
4138

4239
session.onhandshake = mustCall((protocol) => {
43-
ok(protocol);
44-
match(protocol, /DTLS/i);
40+
assert.ok(protocol);
41+
assert.match(protocol, /DTLS/i);
4542
serverHandshakeDone = true;
4643
});
4744
}), {
@@ -52,8 +49,8 @@ const endpoint = listen(mustCall((session) => {
5249
});
5350

5451
const serverAddress = endpoint.address;
55-
ok(serverAddress);
56-
ok(serverAddress.port > 0);
52+
assert.ok(serverAddress);
53+
assert.ok(serverAddress.port > 0);
5754

5855
// Connect client.
5956
const clientSession = connect('127.0.0.1', serverAddress.port, {
@@ -62,18 +59,18 @@ const clientSession = connect('127.0.0.1', serverAddress.port, {
6259
});
6360

6461
clientSession.onmessage = mustCall((data) => {
65-
strictEqual(data.toString(), 'hello from server');
62+
assert.strictEqual(data.toString(), 'hello from server');
6663
clientReceivedData.resolve();
6764
});
6865

6966
clientSession.onhandshake = mustCall((protocol) => {
70-
ok(protocol);
67+
assert.ok(protocol);
7168
clientHandshakeDone = true;
7269
});
7370

7471
// Wait for handshake.
7572
const { protocol } = await clientSession.opened;
76-
match(protocol, /DTLS/i);
73+
assert.match(protocol, /DTLS/i);
7774

7875
// Send data.
7976
clientSession.send('hello from client');
@@ -82,8 +79,8 @@ clientSession.send('hello from client');
8279
await Promise.all([serverReceivedData.promise, clientReceivedData.promise]);
8380

8481
// Verify handshakes completed.
85-
ok(clientHandshakeDone);
86-
ok(serverHandshakeDone);
82+
assert.ok(clientHandshakeDone);
83+
assert.ok(serverHandshakeDone);
8784

8885
// Clean up.
8986
await clientSession.close();

test/parallel/test-dtls-close.mjs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall, mustNotCall } from '../common/index.mjs';
66
import assert from 'node:assert';
77
import * as fixtures from '../common/fixtures.mjs';
88

9-
const { ok, throws } = assert;
10-
const { readKey } = fixtures;
11-
129
if (!hasCrypto) {
1310
skip('missing crypto');
1411
}
@@ -19,9 +16,9 @@ if (!process.features.dtls) {
1916

2017
const { listen, connect } = await import('node:dtls');
2118

22-
const serverCert = readKey('agent1-cert.pem');
23-
const serverKey = readKey('agent1-key.pem');
24-
const ca = readKey('ca1-cert.pem');
19+
const serverCert = fixtures.readKey('agent1-cert.pem');
20+
const serverKey = fixtures.readKey('agent1-key.pem');
21+
const ca = fixtures.readKey('ca1-cert.pem');
2522

2623
// Test 1: Graceful close from client side.
2724
{
@@ -48,7 +45,7 @@ const ca = readKey('ca1-cert.pem');
4845

4946
// Graceful close.
5047
const closedPromise = session.close();
51-
ok(closedPromise instanceof Promise);
48+
assert.ok(closedPromise instanceof Promise);
5249
await closedPromise;
5350

5451
// Wait for server to see the close.
@@ -80,7 +77,7 @@ const ca = readKey('ca1-cert.pem');
8077
session.destroy();
8178

8279
// After destroy, send should fail.
83-
throws(() => {
80+
assert.throws(() => {
8481
session.send('should fail');
8582
}, /destroyed/i);
8683

test/parallel/test-dtls-multiple-clients.mjs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import { hasCrypto, skip, mustCall } from '../common/index.mjs';
66
import assert from 'node:assert';
77
import * as fixtures from '../common/fixtures.mjs';
88

9-
const { strictEqual } = assert;
10-
const { readKey } = fixtures;
11-
129
if (!hasCrypto) {
1310
skip('missing crypto');
1411
}
@@ -19,9 +16,9 @@ if (!process.features.dtls) {
1916

2017
const { listen, connect } = await import('node:dtls');
2118

22-
const serverCert = readKey('agent1-cert.pem');
23-
const serverKey = readKey('agent1-key.pem');
24-
const ca = readKey('ca1-cert.pem');
19+
const serverCert = fixtures.readKey('agent1-cert.pem');
20+
const serverKey = fixtures.readKey('agent1-key.pem');
21+
const ca = fixtures.readKey('ca1-cert.pem');
2522

2623
const NUM_CLIENTS = 3;
2724
let sessionsAccepted = 0;
@@ -57,7 +54,7 @@ for (let i = 0; i < NUM_CLIENTS; i++) {
5754
});
5855

5956
session.onmessage = mustCall((data) => {
60-
strictEqual(data.toString(), `echo:client${i}`);
57+
assert.strictEqual(data.toString(), `echo:client${i}`);
6158
received.resolve();
6259
});
6360

0 commit comments

Comments
 (0)