Skip to content

Commit a721570

Browse files
cjihrigmarco-ippolito
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 99da7d7 commit a721570

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
@@ -11,7 +11,6 @@ const {
1111
FunctionPrototype,
1212
MathMax,
1313
Number,
14-
ObjectEntries,
1514
ObjectSeal,
1615
PromisePrototypeThen,
1716
PromiseResolve,
@@ -103,10 +102,28 @@ function lazyAssertObject() {
103102
if (assertObj === undefined) {
104103
assertObj = new SafeMap();
105104
const assert = require('assert');
106-
for (const { 0: key, 1: value } of ObjectEntries(assert)) {
107-
if (typeof value === 'function') {
108-
assertObj.set(value, key);
109-
}
105+
106+
const methodsToCopy = [
107+
'deepEqual',
108+
'deepStrictEqual',
109+
'doesNotMatch',
110+
'doesNotReject',
111+
'doesNotThrow',
112+
'equal',
113+
'fail',
114+
'ifError',
115+
'match',
116+
'notDeepEqual',
117+
'notDeepStrictEqual',
118+
'notEqual',
119+
'notStrictEqual',
120+
'ok',
121+
'rejects',
122+
'strictEqual',
123+
'throws',
124+
];
125+
for (let i = 0; i < methodsToCopy.length; i++) {
126+
assertObj.set(methodsToCopy[i], assert[methodsToCopy[i]]);
110127
}
111128
}
112129
return assertObj;
@@ -210,18 +227,18 @@ class TestContext {
210227
get assert() {
211228
if (this.#assert === undefined) {
212229
const { plan } = this.#test;
213-
const assertions = lazyAssertObject();
230+
const map = lazyAssertObject();
214231
const assert = { __proto__: null };
215232

216233
this.#assert = assert;
217-
for (const { 0: method, 1: name } of assertions.entries()) {
234+
map.forEach((method, name) => {
218235
assert[name] = (...args) => {
219236
if (plan !== null) {
220237
plan.actual++;
221238
}
222239
return ReflectApply(method, assert, args);
223240
};
224-
}
241+
});
225242
}
226243
return this.#assert;
227244
}

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)