From cc1d944e540360d5fc595871762bf844d82152d0 Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Mon, 20 May 2024 12:49:15 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/52860 PR-URL: https://github.com/nodejs/node/pull/53049 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Chemi Atlow Reviewed-By: Moshe Atlow Reviewed-By: Antoine du Hamel --- lib/internal/test_runner/test.js | 33 ++++++++++++++++++++++------- test/parallel/test-runner-assert.js | 26 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 test/parallel/test-runner-assert.js diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index af4f5d48c309d9e..fd6ed17cbcff96a 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -11,7 +11,6 @@ const { FunctionPrototype, MathMax, Number, - ObjectEntries, ObjectSeal, PromisePrototypeThen, PromiseResolve, @@ -103,10 +102,28 @@ function lazyAssertObject() { if (assertObj === undefined) { assertObj = new SafeMap(); const assert = require('assert'); - for (const { 0: key, 1: value } of ObjectEntries(assert)) { - if (typeof value === 'function') { - assertObj.set(value, key); - } + + const methodsToCopy = [ + 'deepEqual', + 'deepStrictEqual', + 'doesNotMatch', + 'doesNotReject', + 'doesNotThrow', + 'equal', + 'fail', + 'ifError', + 'match', + 'notDeepEqual', + 'notDeepStrictEqual', + 'notEqual', + 'notStrictEqual', + 'ok', + 'rejects', + 'strictEqual', + 'throws', + ]; + for (let i = 0; i < methodsToCopy.length; i++) { + assertObj.set(methodsToCopy[i], assert[methodsToCopy[i]]); } } return assertObj; @@ -210,18 +227,18 @@ class TestContext { get assert() { if (this.#assert === undefined) { const { plan } = this.#test; - const assertions = lazyAssertObject(); + const map = lazyAssertObject(); const assert = { __proto__: null }; this.#assert = assert; - for (const { 0: method, 1: name } of assertions.entries()) { + map.forEach((method, name) => { assert[name] = (...args) => { if (plan !== null) { plan.actual++; } return ReflectApply(method, assert, args); }; - } + }); } return this.#assert; } diff --git a/test/parallel/test-runner-assert.js b/test/parallel/test-runner-assert.js new file mode 100644 index 000000000000000..2af05c95414d793 --- /dev/null +++ b/test/parallel/test-runner-assert.js @@ -0,0 +1,26 @@ +'use strict'; +require('../common'); +const { deepStrictEqual } = require('node:assert'); +const test = require('node:test'); + +test('only methods from node:assert are on t.assert', (t) => { + deepStrictEqual(Object.keys(t.assert).sort(), [ + 'deepEqual', + 'deepStrictEqual', + 'doesNotMatch', + 'doesNotReject', + 'doesNotThrow', + 'equal', + 'fail', + 'ifError', + 'match', + 'notDeepEqual', + 'notDeepStrictEqual', + 'notEqual', + 'notStrictEqual', + 'ok', + 'rejects', + 'strictEqual', + 'throws', + ]); +});