From f51f69bea2a9d802acedff2f32f9975c20a2e552 Mon Sep 17 00:00:00 2001 From: Phillip Markert Date: Thu, 13 Aug 2026 17:46:39 -0400 Subject: [PATCH] test: replace `forEach()` with `for...of` in parallel tests Replace `Array.prototype.forEach()` with `for...of` loops across 17 tests in `test/parallel`, so each loop body reads as a plain statement rather than an arrow callback. None of the iterated values are sparse arrays, the one case where `forEach` and `for...of` genuinely differ, so both constructs visit the same elements in the same order. No callback relied on `this`, an early return, or async behaviour, and the number of assertions run in each file is unchanged. Signed-off-by: Phillip Markert --- test/parallel/test-btoa-atob.js | 11 +++++++---- ...test-child-process-fork-stdio-string-variant.js | 5 ++++- test/parallel/test-child-process-ipc-next-tick.js | 4 ++-- test/parallel/test-debugger-pid.js | 4 +++- ...newprotomethod-remove-unnecessary-prototypes.js | 7 ++++--- .../test-events-uncaught-exception-stack.js | 4 ++-- test/parallel/test-fs-buffertype-writesync.js | 7 ++++--- .../test-fs-cp-sync-verbatim-symlinks-invalid.mjs | 14 +++++++------- test/parallel/test-fs-readlink-type-check.js | 5 +++-- test/parallel/test-fs-rmdir-type-check.js | 5 +++-- test/parallel/test-fs-unlink-type-check.js | 5 +++-- test/parallel/test-http-correct-hostname.js | 4 ++-- test/parallel/test-http-hostname-typechecking.js | 5 +++-- .../test-http-req-close-robust-from-tampering.js | 3 ++- test/parallel/test-http-server-unconsume.js | 5 +++-- .../test-http2-server-settimeout-no-callback.js | 5 +++-- test/parallel/test-http2-status-code-invalid.js | 5 +++-- 17 files changed, 58 insertions(+), 40 deletions(-) diff --git a/test/parallel/test-btoa-atob.js b/test/parallel/test-btoa-atob.js index a2c8d9e3134c..3fde039f395e 100644 --- a/test/parallel/test-btoa-atob.js +++ b/test/parallel/test-btoa-atob.js @@ -26,14 +26,17 @@ assert.strictEqual(atob({ toString: () => '' }), ''); assert.strictEqual(atob({ [Symbol.toPrimitive]: () => '' }), ''); assert.throws(() => atob(Symbol()), /TypeError/); -[ +const testCases = [ undefined, false, () => {}, {}, [1], 0, 1, 0n, 1n, -Infinity, 'a', 'a\n\n\n', '\ra\r\r', ' a ', '\t\t\ta', 'a\f\f\f', '\ta\r \n\f', -].forEach((value) => - // See #2 - https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob +]; + +// See #2 - https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob +for (const value of testCases) { assert.throws(() => atob(value), { constructor: DOMException, name: 'InvalidCharacterError', code: 5, - })); + }); +} diff --git a/test/parallel/test-child-process-fork-stdio-string-variant.js b/test/parallel/test-child-process-fork-stdio-string-variant.js index 6a396b51d9bd..91691b353a15 100644 --- a/test/parallel/test-child-process-fork-stdio-string-variant.js +++ b/test/parallel/test-child-process-fork-stdio-string-variant.js @@ -29,4 +29,7 @@ function test(stringVariant) { child.on('exit', common.mustCall((code) => assert.strictEqual(code, 0))); } -['pipe', 'inherit', 'ignore'].forEach(test); +const testCases = ['pipe', 'inherit', 'ignore']; +for (const value of testCases) { + test(value); +} diff --git a/test/parallel/test-child-process-ipc-next-tick.js b/test/parallel/test-child-process-ipc-next-tick.js index b23aefc85d11..849a927e251e 100644 --- a/test/parallel/test-child-process-ipc-next-tick.js +++ b/test/parallel/test-child-process-ipc-next-tick.js @@ -32,8 +32,8 @@ if (process.argv[2] === 'child') { child.on('message', common.mustCall((msg) => { assert.strictEqual(msg, 'ready'); - values.forEach((value) => { + for (const value of values) { child.send(value); - }); + }; })); } diff --git a/test/parallel/test-debugger-pid.js b/test/parallel/test-debugger-pid.js index 157939c05c73..6fcdac4d9d1d 100644 --- a/test/parallel/test-debugger-pid.js +++ b/test/parallel/test-debugger-pid.js @@ -18,7 +18,9 @@ interfacer.stderr.setEncoding('utf-8'); const onData = (data) => { data = (buffer + data).split('\n'); buffer = data.pop(); - data.forEach((line) => interfacer.emit('line', line)); + for (const line of data) { + interfacer.emit('line', line); + } }; interfacer.stdout.on('data', onData); interfacer.stderr.on('data', onData); diff --git a/test/parallel/test-env-newprotomethod-remove-unnecessary-prototypes.js b/test/parallel/test-env-newprotomethod-remove-unnecessary-prototypes.js index 22c0c8665d14..638c2c3c8722 100644 --- a/test/parallel/test-env-newprotomethod-remove-unnecessary-prototypes.js +++ b/test/parallel/test-env-newprotomethod-remove-unnecessary-prototypes.js @@ -7,13 +7,14 @@ require('../common'); const assert = require('assert'); const { internalBinding } = require('internal/test/binding'); -[ +const testCases = [ internalBinding('udp_wrap').UDP.prototype.bind6, internalBinding('tcp_wrap').TCP.prototype.bind6, internalBinding('udp_wrap').UDP.prototype.send6, internalBinding('tcp_wrap').TCP.prototype.bind, internalBinding('udp_wrap').UDP.prototype.close, internalBinding('tcp_wrap').TCP.prototype.open, -].forEach((binding, i) => { +]; +for (const [i, binding] of testCases.entries()) { assert.strictEqual('prototype' in binding, false, `Test ${i} failed`); -}); +} diff --git a/test/parallel/test-events-uncaught-exception-stack.js b/test/parallel/test-events-uncaught-exception-stack.js index 25fe9d6585f1..c11fcbabcb35 100644 --- a/test/parallel/test-events-uncaught-exception-stack.js +++ b/test/parallel/test-events-uncaught-exception-stack.js @@ -8,9 +8,9 @@ const EventEmitter = require('events'); process.on('uncaughtException', common.mustCall((err) => { const [firstLine, ...lines] = err.stack.split('\n'); assert.strictEqual(firstLine, 'Error'); - lines.forEach((line) => { + for (const line of lines) { assert.match(line, /^ {4}at/); - }); + } })); new EventEmitter().emit('error', new Error()); diff --git a/test/parallel/test-fs-buffertype-writesync.js b/test/parallel/test-fs-buffertype-writesync.js index 5649a00569a2..d1738dd3cc35 100644 --- a/test/parallel/test-fs-buffertype-writesync.js +++ b/test/parallel/test-fs-buffertype-writesync.js @@ -6,11 +6,12 @@ require('../common'); const assert = require('assert'); const fs = require('fs'); -[ +const testCases = [ true, false, 0, 1, Infinity, () => {}, {}, [], undefined, null, -].forEach((value) => { +]; +for (const value of testCases) { assert.throws( () => fs.writeSync(1, value), { message: /"buffer"/, code: 'ERR_INVALID_ARG_TYPE' } ); -}); +} diff --git a/test/parallel/test-fs-cp-sync-verbatim-symlinks-invalid.mjs b/test/parallel/test-fs-cp-sync-verbatim-symlinks-invalid.mjs index 3db176487f71..c9ec4e82f414 100644 --- a/test/parallel/test-fs-cp-sync-verbatim-symlinks-invalid.mjs +++ b/test/parallel/test-fs-cp-sync-verbatim-symlinks-invalid.mjs @@ -8,10 +8,10 @@ import fixtures from '../common/fixtures.js'; tmpdir.refresh(); const src = fixtures.path('copy/kitchen-sink'); -[1, [], {}, null, 1n, undefined, null, Symbol(), '', () => {}] - .forEach((verbatimSymlinks) => { - assert.throws( - () => cpSync(src, src, { verbatimSymlinks }), - { code: 'ERR_INVALID_ARG_TYPE' } - ); - }); +const testCases = [1, [], {}, null, 1n, undefined, null, Symbol(), '', () => {}]; +for (const verbatimSymlinks of testCases) { + assert.throws( + () => cpSync(src, src, { verbatimSymlinks }), + { code: 'ERR_INVALID_ARG_TYPE' } + ); +} diff --git a/test/parallel/test-fs-readlink-type-check.js b/test/parallel/test-fs-readlink-type-check.js index 58d431308c76..adf2c96126e7 100644 --- a/test/parallel/test-fs-readlink-type-check.js +++ b/test/parallel/test-fs-readlink-type-check.js @@ -4,7 +4,8 @@ const common = require('../common'); const assert = require('assert'); const fs = require('fs'); -[false, 1, {}, [], null, undefined].forEach((i) => { +const testCases = [false, 1, {}, [], null, undefined]; +for (const i of testCases) { assert.throws( () => fs.readlink(i, common.mustNotCall()), { @@ -19,4 +20,4 @@ const fs = require('fs'); name: 'TypeError' } ); -}); +} diff --git a/test/parallel/test-fs-rmdir-type-check.js b/test/parallel/test-fs-rmdir-type-check.js index 7014ce27f8e3..321386b0d7d1 100644 --- a/test/parallel/test-fs-rmdir-type-check.js +++ b/test/parallel/test-fs-rmdir-type-check.js @@ -4,7 +4,8 @@ const common = require('../common'); const assert = require('assert'); const fs = require('fs'); -[false, 1, [], {}, null, undefined].forEach((i) => { +const testCases = [false, 1, [], {}, null, undefined]; +for (const i of testCases) { assert.throws( () => fs.rmdir(i, common.mustNotCall()), { @@ -19,4 +20,4 @@ const fs = require('fs'); name: 'TypeError' } ); -}); +} diff --git a/test/parallel/test-fs-unlink-type-check.js b/test/parallel/test-fs-unlink-type-check.js index 006e9ad73485..62c1cf3da71b 100644 --- a/test/parallel/test-fs-unlink-type-check.js +++ b/test/parallel/test-fs-unlink-type-check.js @@ -4,7 +4,8 @@ const common = require('../common'); const assert = require('assert'); const fs = require('fs'); -[false, 1, {}, [], null, undefined].forEach((i) => { +const testCases = [false, 1, {}, [], null, undefined]; +for (const i of testCases) { assert.throws( () => fs.unlink(i, common.mustNotCall()), { @@ -19,4 +20,4 @@ const fs = require('fs'); name: 'TypeError' } ); -}); +} diff --git a/test/parallel/test-http-correct-hostname.js b/test/parallel/test-http-correct-hostname.js index c67a6d49f2e7..ea4b3cb25a09 100644 --- a/test/parallel/test-http-correct-hostname.js +++ b/test/parallel/test-http-correct-hostname.js @@ -15,7 +15,7 @@ if (common.hasCrypto) { modules.https = https; } -Object.keys(modules).forEach((module) => { +for (const module of Object.keys(modules)) { const doNotCall = common.mustNotCall( `${module}.request should not connect to ${module}://example.com%60x.example.com` ); @@ -25,4 +25,4 @@ Object.keys(modules).forEach((module) => { 'example.com`x.example.com', ]); req.abort(); -}); +}; diff --git a/test/parallel/test-http-hostname-typechecking.js b/test/parallel/test-http-hostname-typechecking.js index 368766e08701..c143106b115e 100644 --- a/test/parallel/test-http-hostname-typechecking.js +++ b/test/parallel/test-http-hostname-typechecking.js @@ -8,7 +8,8 @@ const http = require('http'); // when passed as the value of either options.hostname or options.host const vals = [{}, [], NaN, Infinity, -Infinity, true, false, 1, 0, new Date()]; -vals.forEach((v) => { + +for (const v of vals) { const received = common.invalidArgTypeHelper(v); assert.throws( () => http.request({ hostname: v }), @@ -31,7 +32,7 @@ vals.forEach((v) => { received } ); -}); +} // These values are OK and should not throw synchronously. // Only testing for 'hostname' validation so ignore connection errors. diff --git a/test/parallel/test-http-req-close-robust-from-tampering.js b/test/parallel/test-http-req-close-robust-from-tampering.js index edfdb309a7e4..75f57ad54133 100644 --- a/test/parallel/test-http-req-close-robust-from-tampering.js +++ b/test/parallel/test-http-req-close-robust-from-tampering.js @@ -7,7 +7,8 @@ const { connect } = require('net'); // cause an error. const server = createServer(common.mustCall((req, res) => { - req.client._events.close.forEach((fn) => { fn.bind(req)(); }); + const closeHandlers = req.client._events.close; + for (const fn of closeHandlers) { fn.bind(req)(); } })); server.unref(); diff --git a/test/parallel/test-http-server-unconsume.js b/test/parallel/test-http-server-unconsume.js index 0a0b5913812a..e92d7c127504 100644 --- a/test/parallel/test-http-server-unconsume.js +++ b/test/parallel/test-http-server-unconsume.js @@ -4,7 +4,8 @@ const assert = require('assert'); const http = require('http'); const net = require('net'); -['on', 'addListener', 'prependListener'].forEach((testFn) => { +const testCases = ['on', 'addListener', 'prependListener']; +for (const testFn of testCases) { let received = ''; const server = http.createServer(function(req, res) { @@ -30,4 +31,4 @@ const net = require('net'); })); })); })); -}); +}; diff --git a/test/parallel/test-http2-server-settimeout-no-callback.js b/test/parallel/test-http2-server-settimeout-no-callback.js index d0352067b7bd..a5cb080609f5 100644 --- a/test/parallel/test-http2-server-settimeout-no-callback.js +++ b/test/parallel/test-http2-server-settimeout-no-callback.js @@ -11,7 +11,8 @@ const http2 = require('http2'); const verifyCallbacks = common.mustCall((server) => { const testTimeout = 10; - [true, 1, {}, [], null, 'test'].forEach((notFunction) => { + const testCases = [true, 1, {}, [], null, 'test']; + for (const notFunction of testCases) { assert.throws( () => server.setTimeout(testTimeout, notFunction), { @@ -19,7 +20,7 @@ const verifyCallbacks = common.mustCall((server) => { code: 'ERR_INVALID_ARG_TYPE', } ); - }); + }; // No callback const returnedVal = server.setTimeout(testTimeout); diff --git a/test/parallel/test-http2-status-code-invalid.js b/test/parallel/test-http2-status-code-invalid.js index a906c706d7d7..a8b92aad6369 100644 --- a/test/parallel/test-http2-status-code-invalid.js +++ b/test/parallel/test-http2-status-code-invalid.js @@ -19,9 +19,10 @@ function expectsError(code) { server.on('stream', common.mustCall((stream) => { // Anything lower than 100 and greater than 599 is rejected - [ 99, 700, 1000 ].forEach((i) => { + const testCases = [ 99, 700, 1000 ]; + for (const i of testCases) { assert.throws(() => stream.respond({ ':status': i }), expectsError(i)); - }); + } stream.respond(); stream.end();