From 833d98fef059f33bd671377790b5829e29331ca3 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 18 May 2024 12:32:41 -0400 Subject: [PATCH 1/4] 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 --- lib/internal/test_runner/test.js | 37 +++++++++++++++++++++-------- test/parallel/test-runner-assert.js | 26 ++++++++++++++++++++ 2 files changed, 53 insertions(+), 10 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..1333f43d08b28d 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, @@ -105,13 +104,31 @@ function lazyFindSourceMap(file) { 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); - } - } + + assertObj = new SafeMap([ + // eslint-disable-next-line no-restricted-properties + ['deepEqual', assert.deepEqual], + ['deepStrictEqual', assert.deepStrictEqual], + ['doesNotMatch', assert.doesNotMatch], + ['doesNotReject', assert.doesNotReject], + ['doesNotThrow', assert.doesNotThrow], + // eslint-disable-next-line no-restricted-properties + ['equal', assert.equal], + ['fail', assert.fail], + ['ifError', assert.ifError], + ['match', assert.match], + // eslint-disable-next-line no-restricted-properties + ['notDeepEqual', assert.notDeepEqual], + ['notDeepStrictEqual', assert.notDeepStrictEqual], + // eslint-disable-next-line no-restricted-properties + ['notEqual', assert.notEqual], + ['notStrictEqual', assert.notStrictEqual], + ['ok', assert.ok], + ['rejects', assert.rejects], + ['strictEqual', assert.strictEqual], + ['throws', assert.throws], + ]); } 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', + ]); +}); From 59a6d7b7765d6021228c87a3dda6d99244de6664 Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Sat, 18 May 2024 20:59:26 -0400 Subject: [PATCH 2/4] Update lib/internal/test_runner/test.js Co-authored-by: Antoine du Hamel --- lib/internal/test_runner/test.js | 45 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 1333f43d08b28d..c14f76e8abca07 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -106,29 +106,28 @@ function lazyAssertObject() { if (assertObj === undefined) { const assert = require('assert'); - assertObj = new SafeMap([ - // eslint-disable-next-line no-restricted-properties - ['deepEqual', assert.deepEqual], - ['deepStrictEqual', assert.deepStrictEqual], - ['doesNotMatch', assert.doesNotMatch], - ['doesNotReject', assert.doesNotReject], - ['doesNotThrow', assert.doesNotThrow], - // eslint-disable-next-line no-restricted-properties - ['equal', assert.equal], - ['fail', assert.fail], - ['ifError', assert.ifError], - ['match', assert.match], - // eslint-disable-next-line no-restricted-properties - ['notDeepEqual', assert.notDeepEqual], - ['notDeepStrictEqual', assert.notDeepStrictEqual], - // eslint-disable-next-line no-restricted-properties - ['notEqual', assert.notEqual], - ['notStrictEqual', assert.notStrictEqual], - ['ok', assert.ok], - ['rejects', assert.rejects], - ['strictEqual', assert.strictEqual], - ['throws', assert.throws], - ]); + 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; } From 3f687707b88e2633933928ba2248b5e85a79caa1 Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Sat, 18 May 2024 20:59:48 -0400 Subject: [PATCH 3/4] Update lib/internal/test_runner/test.js Co-authored-by: Antoine du Hamel --- lib/internal/test_runner/test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index c14f76e8abca07..0b498e548bcd4d 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -104,6 +104,7 @@ function lazyFindSourceMap(file) { function lazyAssertObject() { if (assertObj === undefined) { + assertObj = new SafeMap(); const assert = require('assert'); const methodsToCopy = [ From f3efcf90110ac7e7da26a17bbaf5a48a940301c6 Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Mon, 20 May 2024 00:21:20 -0400 Subject: [PATCH 4/4] Update lib/internal/test_runner/test.js Co-authored-by: Antoine du Hamel --- lib/internal/test_runner/test.js | 34 ++++++++++++++++---------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 0b498e548bcd4d..effc73c0f2ca7d 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -108,23 +108,23 @@ function lazyAssertObject() { const assert = require('assert'); const methodsToCopy = [ - 'deepEqual', - 'deepStrictEqual', - 'doesNotMatch', - 'doesNotReject', - 'doesNotThrow', - 'equal', - 'fail', - 'ifError', - 'match', - 'notDeepEqual', - 'notDeepStrictEqual', - 'notEqual', - 'notStrictEqual', - 'ok', - 'rejects', - 'strictEqual', - 'throws', + '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]]);