Skip to content

Commit 3ff174f

Browse files
cjihrigtargos
authored andcommitted
test_runner: fix t.assert methods
The node:assert module contains several top level APIs that do not make sense to expose as methods on t.assert. Examples include AssertionError and CallTracker. This commit removes such APIs from t.assert. Refs: #52860 PR-URL: #53049 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent f949080 commit 3ff174f

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

lib/internal/test_runner/test.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const {
1212
MathMax,
1313
Number,
1414
ObjectDefineProperty,
15-
ObjectEntries,
1615
ObjectSeal,
1716
PromisePrototypeThen,
1817
PromiseResolve,
@@ -107,10 +106,28 @@ function lazyAssertObject() {
107106
if (assertObj === undefined) {
108107
assertObj = new SafeMap();
109108
const assert = require('assert');
110-
for (const { 0: key, 1: value } of ObjectEntries(assert)) {
111-
if (typeof value === 'function') {
112-
assertObj.set(value, key);
113-
}
109+
110+
const methodsToCopy = [
111+
'deepEqual',
112+
'deepStrictEqual',
113+
'doesNotMatch',
114+
'doesNotReject',
115+
'doesNotThrow',
116+
'equal',
117+
'fail',
118+
'ifError',
119+
'match',
120+
'notDeepEqual',
121+
'notDeepStrictEqual',
122+
'notEqual',
123+
'notStrictEqual',
124+
'ok',
125+
'rejects',
126+
'strictEqual',
127+
'throws',
128+
];
129+
for (let i = 0; i < methodsToCopy.length; i++) {
130+
assertObj.set(methodsToCopy[i], assert[methodsToCopy[i]]);
114131
}
115132
}
116133
return assertObj;
@@ -227,18 +244,18 @@ class TestContext {
227244
get assert() {
228245
if (this.#assert === undefined) {
229246
const { plan } = this.#test;
230-
const assertions = lazyAssertObject();
247+
const map = lazyAssertObject();
231248
const assert = { __proto__: null };
232249

233250
this.#assert = assert;
234-
for (const { 0: method, 1: name } of assertions.entries()) {
251+
map.forEach((method, name) => {
235252
assert[name] = (...args) => {
236253
if (plan !== null) {
237254
plan.actual++;
238255
}
239256
return ReflectApply(method, assert, args);
240257
};
241-
}
258+
});
242259
}
243260
return this.#assert;
244261
}

test/parallel/test-runner-assert.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
require('../common');
3+
const { deepStrictEqual } = require('node:assert');
4+
const test = require('node:test');
5+
6+
test('only methods from node:assert are on t.assert', (t) => {
7+
deepStrictEqual(Object.keys(t.assert).sort(), [
8+
'deepEqual',
9+
'deepStrictEqual',
10+
'doesNotMatch',
11+
'doesNotReject',
12+
'doesNotThrow',
13+
'equal',
14+
'fail',
15+
'ifError',
16+
'match',
17+
'notDeepEqual',
18+
'notDeepStrictEqual',
19+
'notEqual',
20+
'notStrictEqual',
21+
'ok',
22+
'rejects',
23+
'strictEqual',
24+
'throws',
25+
]);
26+
});

0 commit comments

Comments
 (0)