From f9a09131d76db6ef74b0d8da91ded9a2ab623687 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 da502812f52c3b..effc73c0f2ca7d 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -12,7 +12,6 @@ const { MathMax, Number, ObjectDefineProperty, - ObjectEntries, ObjectSeal, PromisePrototypeThen, PromiseResolve, @@ -107,10 +106,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; @@ -227,18 +244,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 00000000000000..2af05c95414d79 --- /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', + ]); +});