diff --git a/test/eslint.config_partial.mjs b/test/eslint.config_partial.mjs index d19e98a4bf3131..800c8bb036bad4 100644 --- a/test/eslint.config_partial.mjs +++ b/test/eslint.config_partial.mjs @@ -190,6 +190,10 @@ export default [ 'wasm-allocation', 'wpt', ].join(',')}}/**/*.{js,mjs,cjs}`, + `test/parallel/test-{${ + // 0x61 is code for 'a', this generates a string enumerating latin letters: 'a*,b*,…' + Array.from({ length: 2 }, (_, i) => String.fromCharCode(0x61 + i, 42)).join(',') + }}.{js,mjs,cjs}`, ], rules: { 'node-core/must-call-assert': 'error', diff --git a/test/parallel/test-abortcontroller-internal.js b/test/parallel/test-abortcontroller-internal.js index 4347d308de3545..63dd26c36e32a0 100644 --- a/test/parallel/test-abortcontroller-internal.js +++ b/test/parallel/test-abortcontroller-internal.js @@ -2,9 +2,7 @@ 'use strict'; require('../common'); -const { - strictEqual, -} = require('assert'); +const assert = require('assert'); const { test, @@ -30,5 +28,5 @@ test('A weak event listener should not prevent gc', async () => { await sleep(10); globalThis.gc(); - strictEqual(ref.deref(), undefined); + assert.strictEqual(ref.deref(), undefined); }); diff --git a/test/parallel/test-abortcontroller.js b/test/parallel/test-abortcontroller.js index 87781f849ffb52..948bd208cd2b90 100644 --- a/test/parallel/test-abortcontroller.js +++ b/test/parallel/test-abortcontroller.js @@ -1,15 +1,10 @@ // Flags: --expose-gc 'use strict'; -require('../common'); +const common = require('../common'); const { inspect } = require('util'); -const { - ok, - notStrictEqual, - strictEqual, - throws, -} = require('assert'); +const assert = require('assert'); const { test, @@ -25,34 +20,34 @@ const { setTimeout: sleep } = require('timers/promises'); test('Abort is fired with the correct event type on AbortControllers', () => { // Tests that abort is fired with the correct event type on AbortControllers const ac = new AbortController(); - ok(ac.signal); + assert.ok(ac.signal); - const fn = mock.fn((event) => { - ok(event); - strictEqual(event.type, 'abort'); - }); + const fn = mock.fn(common.mustCall((event) => { + assert.ok(event); + assert.strictEqual(event.type, 'abort'); + }, 2)); ac.signal.onabort = fn; ac.signal.addEventListener('abort', fn); ac.abort(); ac.abort(); - ok(ac.signal.aborted); + assert.ok(ac.signal.aborted); - strictEqual(fn.mock.calls.length, 2); + assert.strictEqual(fn.mock.calls.length, 2); }); test('Abort events are trusted', () => { // Tests that abort events are trusted const ac = new AbortController(); - const fn = mock.fn((event) => { - ok(event.isTrusted); - }); + const fn = mock.fn(common.mustCall((event) => { + assert.ok(event.isTrusted); + })); ac.signal.onabort = fn; ac.abort(); - strictEqual(fn.mock.calls.length, 1); + assert.strictEqual(fn.mock.calls.length, 1); }); test('Abort events have the same isTrusted reference', () => { @@ -73,14 +68,14 @@ test('Abort events have the same isTrusted reference', () => { const firstTrusted = Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev1), 'isTrusted').get; const secondTrusted = Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev2), 'isTrusted').get; const untrusted = Reflect.getOwnPropertyDescriptor(Object.getPrototypeOf(ev3), 'isTrusted').get; - strictEqual(firstTrusted, secondTrusted); - strictEqual(untrusted, firstTrusted); + assert.strictEqual(firstTrusted, secondTrusted); + assert.strictEqual(untrusted, firstTrusted); }); test('AbortSignal is impossible to construct manually', () => { // Tests that AbortSignal is impossible to construct manually const ac = new AbortController(); - throws(() => new ac.signal.constructor(), { + assert.throws(() => new ac.signal.constructor(), { code: 'ERR_ILLEGAL_CONSTRUCTOR', }); }); @@ -89,13 +84,13 @@ test('Symbol.toStringTag is correct', () => { // Symbol.toStringTag const toString = (o) => Object.prototype.toString.call(o); const ac = new AbortController(); - strictEqual(toString(ac), '[object AbortController]'); - strictEqual(toString(ac.signal), '[object AbortSignal]'); + assert.strictEqual(toString(ac), '[object AbortController]'); + assert.strictEqual(toString(ac.signal), '[object AbortSignal]'); }); test('AbortSignal.abort() creates an already aborted signal', () => { const signal = AbortSignal.abort(); - ok(signal.aborted); + assert.ok(signal.aborted); }); test('AbortController properties and methods valiate the receiver', () => { @@ -106,7 +101,7 @@ test('AbortController properties and methods valiate the receiver', () => { const acAbort = AbortController.prototype.abort; const goodController = new AbortController(); - ok(acSignalGet.call(goodController)); + assert.ok(acSignalGet.call(goodController)); acAbort.call(goodController); const badAbortControllers = [ @@ -119,11 +114,11 @@ test('AbortController properties and methods valiate the receiver', () => { { __proto__: AbortController.prototype }, ]; for (const badController of badAbortControllers) { - throws( + assert.throws( () => acSignalGet.call(badController), { name: 'TypeError' } ); - throws( + assert.throws( () => acAbort.call(badController), { name: 'TypeError' } ); @@ -137,7 +132,7 @@ test('AbortSignal properties validate the receiver', () => { ).get; const goodSignal = new AbortController().signal; - strictEqual(signalAbortedGet.call(goodSignal), false); + assert.strictEqual(signalAbortedGet.call(goodSignal), false); const badAbortSignals = [ null, @@ -149,7 +144,7 @@ test('AbortSignal properties validate the receiver', () => { { __proto__: AbortSignal.prototype }, ]; for (const badSignal of badAbortSignals) { - throws( + assert.throws( () => signalAbortedGet.call(badSignal), { name: 'TypeError' } ); @@ -158,38 +153,38 @@ test('AbortSignal properties validate the receiver', () => { test('AbortController inspection depth 1 or null works', () => { const ac = new AbortController(); - strictEqual(inspect(ac, { depth: 1 }), - 'AbortController { signal: [AbortSignal] }'); - strictEqual(inspect(ac, { depth: null }), - 'AbortController { signal: AbortSignal { aborted: false } }'); + assert.strictEqual(inspect(ac, { depth: 1 }), + 'AbortController { signal: [AbortSignal] }'); + assert.strictEqual(inspect(ac, { depth: null }), + 'AbortController { signal: AbortSignal { aborted: false } }'); }); test('AbortSignal reason is set correctly', () => { // Test AbortSignal.reason const ac = new AbortController(); ac.abort('reason'); - strictEqual(ac.signal.reason, 'reason'); + assert.strictEqual(ac.signal.reason, 'reason'); }); test('AbortSignal reasonable is set correctly with AbortSignal.abort()', () => { // Test AbortSignal.reason const signal = AbortSignal.abort('reason'); - strictEqual(signal.reason, 'reason'); + assert.strictEqual(signal.reason, 'reason'); }); test('AbortSignal.timeout() works as expected', async () => { // Test AbortSignal timeout const signal = AbortSignal.timeout(10); - ok(!signal.aborted); + assert.ok(!signal.aborted); const { promise, resolve } = Promise.withResolvers(); - const fn = mock.fn(() => { - ok(signal.aborted); - strictEqual(signal.reason.name, 'TimeoutError'); - strictEqual(signal.reason.code, 23); + const fn = mock.fn(common.mustCall(() => { + assert.ok(signal.aborted); + assert.strictEqual(signal.reason.name, 'TimeoutError'); + assert.strictEqual(signal.reason.code, 23); resolve(); - }); + })); setTimeout(fn, 20); await promise; @@ -205,7 +200,7 @@ test('AbortSignal.timeout() does not prevent the signal from being collected', a await sleep(10); globalThis.gc(); - strictEqual(ref.deref(), undefined); + assert.strictEqual(ref.deref(), undefined); }); test('AbortSignal with a timeout is not collected while there is an active listener', async () => { @@ -220,14 +215,14 @@ test('AbortSignal with a timeout is not collected while there is an active liste await sleep(10); globalThis.gc(); - notStrictEqual(ref.deref(), undefined); - ok(ref.deref() instanceof AbortSignal); + assert.notStrictEqual(ref.deref(), undefined); + assert.ok(ref.deref() instanceof AbortSignal); ref.deref().removeEventListener('abort', handler); await sleep(10); globalThis.gc(); - strictEqual(ref.deref(), undefined); + assert.strictEqual(ref.deref(), undefined); }); test('Setting a long timeout should not keep the process open', () => { @@ -237,18 +232,18 @@ test('Setting a long timeout should not keep the process open', () => { test('AbortSignal.reason should default', () => { // Test AbortSignal.reason default const signal = AbortSignal.abort(); - ok(signal.reason instanceof DOMException); - strictEqual(signal.reason.code, 20); + assert.ok(signal.reason instanceof DOMException); + assert.strictEqual(signal.reason.code, 20); const ac = new AbortController(); ac.abort(); - ok(ac.signal.reason instanceof DOMException); - strictEqual(ac.signal.reason.code, 20); + assert.ok(ac.signal.reason instanceof DOMException); + assert.strictEqual(ac.signal.reason.code, 20); }); test('abortSignal.throwIfAborted() works as expected', () => { // Test abortSignal.throwIfAborted() - throws(() => AbortSignal.abort().throwIfAborted(), { + assert.throws(() => AbortSignal.abort().throwIfAborted(), { code: 20, name: 'AbortError', }); @@ -262,7 +257,7 @@ test('abortSignal.throwIfAobrted() works as expected (2)', () => { const originalDesc = Reflect.getOwnPropertyDescriptor(AbortSignal.prototype, 'aborted'); const actualReason = new Error(); Reflect.defineProperty(AbortSignal.prototype, 'aborted', { value: false }); - throws(() => AbortSignal.abort(actualReason).throwIfAborted(), actualReason); + assert.throws(() => AbortSignal.abort(actualReason).throwIfAborted(), actualReason); Reflect.defineProperty(AbortSignal.prototype, 'aborted', originalDesc); }); @@ -271,6 +266,6 @@ test('abortSignal.throwIfAobrted() works as expected (3)', () => { const actualReason = new Error(); const fakeExcuse = new Error(); Reflect.defineProperty(AbortSignal.prototype, 'reason', { value: fakeExcuse }); - throws(() => AbortSignal.abort(actualReason).throwIfAborted(), actualReason); + assert.throws(() => AbortSignal.abort(actualReason).throwIfAborted(), actualReason); Reflect.defineProperty(AbortSignal.prototype, 'reason', originalDesc); }); diff --git a/test/parallel/test-aborted-util.js b/test/parallel/test-aborted-util.js index 0566204ccdb074..f352cacb48c652 100644 --- a/test/parallel/test-aborted-util.js +++ b/test/parallel/test-aborted-util.js @@ -1,13 +1,9 @@ // Flags: --expose-gc 'use strict'; -require('../common'); +const common = require('../common'); const { aborted } = require('util'); -const { - match, - rejects, - strictEqual, -} = require('assert'); +const assert = require('assert'); const { getEventListeners } = require('events'); const { inspect } = require('util'); @@ -20,8 +16,8 @@ test('Aborted works when provided a resource', async () => { const promise = aborted(ac.signal, {}); ac.abort(); await promise; - strictEqual(ac.signal.aborted, true); - strictEqual(getEventListeners(ac.signal, 'abort').length, 0); + assert.strictEqual(ac.signal.aborted, true); + assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0); }); test('Aborted with gc cleanup', async () => { @@ -31,23 +27,23 @@ test('Aborted with gc cleanup', async () => { const abortedPromise = aborted(ac.signal, {}); const { promise, resolve } = Promise.withResolvers(); - setImmediate(() => { + setImmediate(common.mustCall(() => { globalThis.gc(); ac.abort(); - strictEqual(ac.signal.aborted, true); - strictEqual(getEventListeners(ac.signal, 'abort').length, 0); + assert.strictEqual(ac.signal.aborted, true); + assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0); resolve(); - }); + })); await promise; // Ensure that the promise is still pending - match(inspect(abortedPromise), //); + assert.match(inspect(abortedPromise), //); }); test('Fails with error if not provided AbortSignal', async () => { await Promise.all([{}, null, undefined, Symbol(), [], 1, 0, 1n, true, false, 'a', () => {}].map((sig) => - rejects(aborted(sig, {}), { + assert.rejects(aborted(sig, {}), { code: 'ERR_INVALID_ARG_TYPE', }) )); @@ -57,7 +53,7 @@ test('Fails if not provided a resource', async () => { // Fails if not provided a resource const ac = new AbortController(); await Promise.all([null, undefined, 0, 1, 0n, 1n, Symbol(), '', 'a'].map((resource) => - rejects(aborted(ac.signal, resource), { + assert.rejects(aborted(ac.signal, resource), { code: 'ERR_INVALID_ARG_TYPE', }) )); @@ -82,10 +78,10 @@ function lazySpawn() { test('Does not hang forever', { skip: !lazySpawn() }, async () => { const { promise, resolve } = Promise.withResolvers(); const childProcess = spawn(process.execPath, ['--input-type=module']); - childProcess.on('exit', (code) => { - strictEqual(code, 13); + childProcess.on('exit', common.mustCall((code) => { + assert.strictEqual(code, 13); resolve(); - }); + })); childProcess.stdin.end(` import { aborted } from 'node:util'; await aborted(new AbortController().signal, {}); diff --git a/test/parallel/test-abortsignal-cloneable.js b/test/parallel/test-abortsignal-cloneable.js index cfd194909db38e..7a90cbde5e13ab 100644 --- a/test/parallel/test-abortsignal-cloneable.js +++ b/test/parallel/test-abortsignal-cloneable.js @@ -1,7 +1,7 @@ 'use strict'; -require('../common'); -const { ok, strictEqual } = require('assert'); +const common = require('../common'); +const assert = require('assert'); const { setImmediate: sleep } = require('timers/promises'); const { transferableAbortSignal, @@ -25,13 +25,13 @@ test('Can create a transferable abort controller', async () => { const abort3 = Promise.withResolvers(); const abortResolvers = [abort1, abort2, abort3]; - mc.port1.onmessage = ({ data }) => { - data.addEventListener('abort', () => { - strictEqual(data.reason, 'boom'); + mc.port1.onmessage = common.mustCallAtLeast(({ data }) => { + data.addEventListener('abort', common.mustCall(() => { + assert.strictEqual(data.reason, 'boom'); abortResolvers.shift().resolve(); - }); + })); setupResolvers.shift().resolve(); - }; + }); mc.port2.postMessage(ac.signal, [ac.signal]); @@ -40,10 +40,10 @@ test('Can create a transferable abort controller', async () => { // Although we're using transfer semantics, the local AbortSignal // is still usable locally. - ac.signal.addEventListener('abort', () => { - strictEqual(ac.signal.reason, 'boom'); + ac.signal.addEventListener('abort', common.mustCall(() => { + assert.strictEqual(ac.signal.reason, 'boom'); abortResolvers.shift().resolve(); - }); + })); await Promise.all([ setup1.promise, setup2.promise ]); @@ -57,16 +57,16 @@ test('Can create a transferable abort controller', async () => { test('Can create a transferable abort signal', async () => { const signal = transferableAbortSignal(AbortSignal.abort('boom')); - ok(signal.aborted); - strictEqual(signal.reason, 'boom'); + assert.ok(signal.aborted); + assert.strictEqual(signal.reason, 'boom'); const mc = new MessageChannel(); const { promise, resolve } = Promise.withResolvers(); - mc.port1.onmessage = ({ data }) => { - ok(data instanceof AbortSignal); - ok(data.aborted); - strictEqual(data.reason, 'boom'); + mc.port1.onmessage = common.mustCallAtLeast(({ data }) => { + assert.ok(data instanceof AbortSignal); + assert.ok(data.aborted); + assert.strictEqual(data.reason, 'boom'); resolve(); - }; + }); mc.port2.postMessage(signal, [signal]); await promise; mc.port1.close(); @@ -86,6 +86,6 @@ test('A cloned AbortSignal does not keep the event loop open', async () => { // the loop runs long enough for the test to complete. await sleep(); await sleep(); - strictEqual(fn.mock.calls.length, 1); + assert.strictEqual(fn.mock.calls.length, 1); mc.port2.close(); }); diff --git a/test/parallel/test-als-defaultvalue-original.js b/test/parallel/test-als-defaultvalue-original.js index 7ea5ec0eab45c2..1bfe239abfcf69 100644 --- a/test/parallel/test-als-defaultvalue-original.js +++ b/test/parallel/test-als-defaultvalue-original.js @@ -7,26 +7,23 @@ const { AsyncLocalStorage, } = require('async_hooks'); -const { - strictEqual, - throws, -} = require('assert'); +const assert = require('assert'); // ============================================================================ // The defaultValue option const als1 = new AsyncLocalStorage(); -strictEqual(als1.getStore(), undefined, 'value should be undefined'); +assert.strictEqual(als1.getStore(), undefined); const als2 = new AsyncLocalStorage({ defaultValue: 'default' }); -strictEqual(als2.getStore(), 'default', 'value should be "default"'); +assert.strictEqual(als2.getStore(), 'default'); const als3 = new AsyncLocalStorage({ defaultValue: 42 }); -strictEqual(als3.getStore(), 42, 'value should be 42'); +assert.strictEqual(als3.getStore(), 42); const als4 = new AsyncLocalStorage({ defaultValue: null }); -strictEqual(als4.getStore(), null, 'value should be null'); +assert.strictEqual(als4.getStore(), null); -throws(() => new AsyncLocalStorage(null), { +assert.throws(() => new AsyncLocalStorage(null), { code: 'ERR_INVALID_ARG_TYPE', }); @@ -34,7 +31,7 @@ throws(() => new AsyncLocalStorage(null), { // The name option const als5 = new AsyncLocalStorage({ name: 'test' }); -strictEqual(als5.name, 'test'); +assert.strictEqual(als5.name, 'test'); const als6 = new AsyncLocalStorage(); -strictEqual(als6.name, ''); +assert.strictEqual(als6.name, ''); diff --git a/test/parallel/test-als-defaultvalue.js b/test/parallel/test-als-defaultvalue.js index 0e905104f27c6b..5b5b43b57a72e6 100644 --- a/test/parallel/test-als-defaultvalue.js +++ b/test/parallel/test-als-defaultvalue.js @@ -7,26 +7,23 @@ const { AsyncLocalStorage, } = require('async_hooks'); -const { - strictEqual, - throws, -} = require('assert'); +const assert = require('assert'); // ============================================================================ // The defaultValue option const als1 = new AsyncLocalStorage(); -strictEqual(als1.getStore(), undefined, 'value should be undefined'); +assert.strictEqual(als1.getStore(), undefined); const als2 = new AsyncLocalStorage({ defaultValue: 'default' }); -strictEqual(als2.getStore(), 'default', 'value should be "default"'); +assert.strictEqual(als2.getStore(), 'default'); const als3 = new AsyncLocalStorage({ defaultValue: 42 }); -strictEqual(als3.getStore(), 42, 'value should be 42'); +assert.strictEqual(als3.getStore(), 42); const als4 = new AsyncLocalStorage({ defaultValue: null }); -strictEqual(als4.getStore(), null, 'value should be null'); +assert.strictEqual(als4.getStore(), null); -throws(() => new AsyncLocalStorage(null), { +assert.throws(() => new AsyncLocalStorage(null), { code: 'ERR_INVALID_ARG_TYPE', }); @@ -34,7 +31,7 @@ throws(() => new AsyncLocalStorage(null), { // The name option const als5 = new AsyncLocalStorage({ name: 'test' }); -strictEqual(als5.name, 'test'); +assert.strictEqual(als5.name, 'test'); const als6 = new AsyncLocalStorage(); -strictEqual(als6.name, ''); +assert.strictEqual(als6.name, ''); diff --git a/test/parallel/test-assert-async.js b/test/parallel/test-assert-async.js index bcf3d55622b863..3fc7e3c0a0ea88 100644 --- a/test/parallel/test-assert-async.js +++ b/test/parallel/test-assert-async.js @@ -85,7 +85,7 @@ const invalidThenableFunc = () => { } { - const handler = (err) => { + const handler = common.mustCallAtLeast((err) => { assert(err instanceof assert.AssertionError, `${err.name} is not instance of AssertionError`); assert.strictEqual(err.code, 'ERR_ASSERTION'); @@ -94,7 +94,7 @@ const invalidThenableFunc = () => { assert.strictEqual(err.operator, 'rejects'); assert.ok(!err.stack.includes('at Function.rejects')); return true; - }; + }); let promise = assert.rejects(async () => {}, common.mustNotCall()); promises.push(assert.rejects(promise, common.mustCall(handler))); @@ -132,14 +132,14 @@ promises.push(assert.rejects( )); { - const handler = (generated, actual, err) => { + const handler = common.mustCallAtLeast((generated, actual, err) => { assert.strictEqual(err.generatedMessage, generated); assert.strictEqual(err.code, 'ERR_ASSERTION'); assert.strictEqual(err.actual, actual); assert.strictEqual(err.operator, 'rejects'); assert.match(err.stack, /rejects/); return true; - }; + }); const err = new Error(); promises.push(assert.rejects( assert.rejects(Promise.reject(null), { code: 'FOO' }), @@ -192,14 +192,14 @@ promises.push(assert.rejects( }) ); - const handler1 = (err) => { + const handler1 = common.mustCallAtLeast((err) => { assert(err instanceof assert.AssertionError, `${err.name} is not instance of AssertionError`); assert.strictEqual(err.code, 'ERR_ASSERTION'); assert.strictEqual(err.message, 'Failed'); return true; - }; - const handler2 = (err) => { + }); + const handler2 = common.mustCallAtLeast((err) => { assert(err instanceof assert.AssertionError, `${err.name} is not instance of AssertionError`); assert.strictEqual(err.code, 'ERR_ASSERTION'); @@ -209,7 +209,7 @@ promises.push(assert.rejects( assert.ok(err.stack); assert.ok(!err.stack.includes('at Function.doesNotReject')); return true; - }; + }); const rejectingFn = async () => assert.fail(); diff --git a/test/parallel/test-assert-class-destructuring.js b/test/parallel/test-assert-class-destructuring.js index eb353f095d2da2..ac82ab58631269 100644 --- a/test/parallel/test-assert-class-destructuring.js +++ b/test/parallel/test-assert-class-destructuring.js @@ -2,6 +2,7 @@ require('../common'); const assert = require('assert'); +// eslint-disable-next-line node-core/must-call-assert const { Assert } = require('assert'); const { test } = require('node:test'); @@ -18,18 +19,12 @@ test('Assert class destructuring behavior - diff option', () => { assertInstanceFull.throws( () => assertInstanceFull.strictEqual({ a: 1 }, { a: 2 }), - (err) => { - assert.strictEqual(err.diff, 'full'); - return true; - } + { diff: 'full' }, ); assertInstanceSimple.throws( () => assertInstanceSimple.strictEqual({ a: 1 }, { a: 2 }), - (err) => { - assert.strictEqual(err.diff, 'simple'); - return true; - } + { diff: 'simple' }, ); const { strictEqual: strictEqualSimple } = assertInstanceSimple; diff --git a/test/parallel/test-assert-class.js b/test/parallel/test-assert-class.js index 41271af65ffcbe..cccc7a2f36fff9 100644 --- a/test/parallel/test-assert-class.js +++ b/test/parallel/test-assert-class.js @@ -2,6 +2,7 @@ require('../common'); const assert = require('assert'); +// eslint-disable-next-line node-core/must-call-assert const { Assert } = require('assert'); const { inspect } = require('util'); const { test } = require('node:test'); diff --git a/test/parallel/test-assert-deep.js b/test/parallel/test-assert-deep.js index 6a19e4d0877988..d458a272f2bd61 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -1,6 +1,6 @@ 'use strict'; -const { hasCrypto } = require('../common'); +const { mustCall, hasCrypto } = require('../common'); const assert = require('assert'); const util = require('util'); const { test } = require('node:test'); @@ -223,10 +223,10 @@ function assertNotDeepOrStrict(a, b, err, options) { () => assert.deepStrictEqual(b, a), err || { code: 'ERR_ASSERTION' } ); - const partial = () => { + const partial = mustCall(() => { assert.partialDeepStrictEqual(b, a); assert.partialDeepStrictEqual(a, b); - }; + }); if (options?.partial === 'pass') { partial(); } else { diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index e7e80b830b030d..c6da37d88c7128 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -491,7 +491,26 @@ test('Custom errors', () => { }); test('Verify that throws() and doesNotThrow() throw on non-functions', () => { - const testBlockTypeError = (method, fn) => { + [ + [assert.throws, 'string'], + [assert.doesNotThrow, 'string'], + [assert.throws, 1], + [assert.doesNotThrow, 1], + [assert.throws, true], + [assert.doesNotThrow, true], + [assert.throws, false], + [assert.doesNotThrow, false], + [assert.throws, []], + [assert.doesNotThrow, []], + [assert.throws, {}], + [assert.doesNotThrow, {}], + [assert.throws, /foo/], + [assert.doesNotThrow, /foo/], + [assert.throws, null], + [assert.doesNotThrow, null], + [assert.throws, undefined], + [assert.doesNotThrow, undefined], + ].forEach(([method, fn]) => { assert.throws( () => method(fn), { @@ -501,26 +520,7 @@ test('Verify that throws() and doesNotThrow() throw on non-functions', () => { invalidArgTypeHelper(fn) } ); - }; - - testBlockTypeError(assert.throws, 'string'); - testBlockTypeError(assert.doesNotThrow, 'string'); - testBlockTypeError(assert.throws, 1); - testBlockTypeError(assert.doesNotThrow, 1); - testBlockTypeError(assert.throws, true); - testBlockTypeError(assert.doesNotThrow, true); - testBlockTypeError(assert.throws, false); - testBlockTypeError(assert.doesNotThrow, false); - testBlockTypeError(assert.throws, []); - testBlockTypeError(assert.doesNotThrow, []); - testBlockTypeError(assert.throws, {}); - testBlockTypeError(assert.doesNotThrow, {}); - testBlockTypeError(assert.throws, /foo/); - testBlockTypeError(assert.doesNotThrow, /foo/); - testBlockTypeError(assert.throws, null); - testBlockTypeError(assert.doesNotThrow, null); - testBlockTypeError(assert.throws, undefined); - testBlockTypeError(assert.doesNotThrow, undefined); + }); }); test('https://github.com/nodejs/node/issues/3275', () => { @@ -582,6 +582,7 @@ test('NaN is handled correctly', () => { }); test('Test strict assert', () => { + // eslint-disable-next-line node-core/must-call-assert const { strict } = require('assert'); strict.throws(() => strict.equal(1, true), strict.AssertionError); @@ -1591,6 +1592,7 @@ test('Additional assert', () => { }); test('assert/strict exists', () => { + // eslint-disable-next-line node-core/must-call-assert assert.strictEqual(require('assert/strict'), assert.strict); }); diff --git a/test/parallel/test-async-hooks-destroy-on-gc.js b/test/parallel/test-async-hooks-destroy-on-gc.js index dd7eef8776cdf3..a401f6651ccc20 100644 --- a/test/parallel/test-async-hooks-destroy-on-gc.js +++ b/test/parallel/test-async-hooks-destroy-on-gc.js @@ -21,7 +21,7 @@ let asyncId = null; asyncId = res.asyncId(); } -setImmediate(() => { +setImmediate(common.mustCall(() => { globalThis.gc(); - setImmediate(() => assert.ok(destroyedIds.has(asyncId))); -}); + setImmediate(common.mustCall(() => assert.ok(destroyedIds.has(asyncId)))); +})); diff --git a/test/parallel/test-async-hooks-enable-before-promise-resolve.js b/test/parallel/test-async-hooks-enable-before-promise-resolve.js index 4f6e65df9d29e3..e6facfb6392e29 100644 --- a/test/parallel/test-async-hooks-enable-before-promise-resolve.js +++ b/test/parallel/test-async-hooks-enable-before-promise-resolve.js @@ -9,13 +9,13 @@ const async_hooks = require('async_hooks'); let initialAsyncId; const promise = new Promise((resolve) => { - setTimeout(() => { + setTimeout(common.mustCall(() => { initialAsyncId = async_hooks.executionAsyncId(); async_hooks.createHook({ after: common.mustCall(2) }).enable(); resolve(); - }, 0); + }), 0); }); promise.then(common.mustCall(() => { diff --git a/test/parallel/test-async-hooks-http-parser-destroy.js b/test/parallel/test-async-hooks-http-parser-destroy.js index 6cbc2043b09583..b2d1fd9a94f1de 100644 --- a/test/parallel/test-async-hooks-http-parser-destroy.js +++ b/test/parallel/test-async-hooks-http-parser-destroy.js @@ -44,12 +44,12 @@ async_hooks.createHook({ } }).enable(); -const server = http.createServer((req, res) => { +const server = http.createServer(common.mustCallAtLeast((req, res) => { req.on('close', common.mustCall(() => { req.on('readable', common.mustNotCall()); })); res.end('Hello'); -}); +})); let keepAliveAgent = new http.Agent({ keepAlive: true, diff --git a/test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js b/test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js index bc4ac86e7f1ca1..d0d5ee76e9d115 100644 --- a/test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js +++ b/test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js @@ -1,5 +1,5 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const async_hooks = require('async_hooks'); @@ -7,14 +7,14 @@ const async_hooks = require('async_hooks'); function recurse(n) { const a = new async_hooks.AsyncResource('foobar'); - a.runInAsyncScope(() => { + a.runInAsyncScope(common.mustCall(() => { assert.strictEqual(a.asyncId(), async_hooks.executionAsyncId()); assert.strictEqual(a.triggerAsyncId(), async_hooks.triggerAsyncId()); if (n >= 0) recurse(n - 1); assert.strictEqual(a.asyncId(), async_hooks.executionAsyncId()); assert.strictEqual(a.triggerAsyncId(), async_hooks.triggerAsyncId()); - }); + })); } recurse(1000); diff --git a/test/parallel/test-async-local-storage-contexts.js b/test/parallel/test-async-local-storage-contexts.js index 9a6327133794f6..b63d383fecc4e5 100644 --- a/test/parallel/test-async-local-storage-contexts.js +++ b/test/parallel/test-async-local-storage-contexts.js @@ -1,6 +1,6 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const vm = require('vm'); const { AsyncLocalStorage } = require('async_hooks'); @@ -26,10 +26,10 @@ vm.runInContext(` const storage = new AsyncLocalStorage(); async function test() { - return storage.run({ test: 'main context' }, async () => { + return storage.run({ test: 'main context' }, common.mustCall(async () => { assert.strictEqual(storage.getStore().test, 'main context'); await 42; assert.strictEqual(storage.getStore().test, 'main context'); - }); + })); } test(); diff --git a/test/parallel/test-async-local-storage-snapshot.js b/test/parallel/test-async-local-storage-snapshot.js index 63e47ba3ce0ac0..169da60c3a3286 100644 --- a/test/parallel/test-async-local-storage-snapshot.js +++ b/test/parallel/test-async-local-storage-snapshot.js @@ -1,7 +1,7 @@ 'use strict'; const common = require('../common'); -const { strictEqual } = require('assert'); +const assert = require('assert'); const { AsyncLocalStorage } = require('async_hooks'); const asyncLocalStorage = new AsyncLocalStorage(); @@ -13,4 +13,4 @@ const result = return asyncLocalStorage.getStore(); }); })); -strictEqual(result, 123); +assert.strictEqual(result, 123); diff --git a/test/parallel/test-async-wrap-promise-after-enabled.js b/test/parallel/test-async-wrap-promise-after-enabled.js index cbca873574c1f8..afd049192bd57b 100644 --- a/test/parallel/test-async-wrap-promise-after-enabled.js +++ b/test/parallel/test-async-wrap-promise-after-enabled.js @@ -32,8 +32,8 @@ const hooks = async_hooks.createHook({ }) }); -setImmediate(() => { +setImmediate(common.mustCall(() => { assert.deepStrictEqual(seenEvents, ['before', 'then', 'after']); -}); +})); hooks.enable(); // After `setImmediate` in order to not catch its init event. diff --git a/test/parallel/test-async-wrap-tlssocket-asyncreset.js b/test/parallel/test-async-wrap-tlssocket-asyncreset.js index e0dd8be2904429..513911978b1dd3 100644 --- a/test/parallel/test-async-wrap-tlssocket-asyncreset.js +++ b/test/parallel/test-async-wrap-tlssocket-asyncreset.js @@ -42,7 +42,7 @@ server.listen( res.socket.on('error', (err) => assert.fail(err)); res.resume(); // Drain the socket and wait for it to be free to reuse - res.socket.once('free', () => { + res.socket.once('free', common.mustCall(() => { // This is the pain point. Internally the Agent will call // `socket._handle.asyncReset()` and if the _handle does not implement // `asyncReset` this will throw TypeError @@ -58,7 +58,7 @@ server.listen( }) ); req2.on('error', (err) => assert.fail(err)); - }); + })); }) ); req.on('error', (err) => assert.fail(err)); diff --git a/test/parallel/test-asyncresource-bind.js b/test/parallel/test-asyncresource-bind.js index 0d1f5fb62ab9eb..fb47a04b1ff1e9 100644 --- a/test/parallel/test-asyncresource-bind.js +++ b/test/parallel/test-asyncresource-bind.js @@ -8,10 +8,10 @@ const fn = common.mustCall(AsyncResource.bind(() => { return executionAsyncId(); })); -setImmediate(() => { +setImmediate(common.mustCall(() => { const asyncId = executionAsyncId(); assert.notStrictEqual(asyncId, fn()); -}); +})); const asyncResource = new AsyncResource('test'); @@ -27,11 +27,11 @@ const fn2 = asyncResource.bind((a, b) => { assert.strictEqual(fn2.length, 2); -setImmediate(() => { +setImmediate(common.mustCall(() => { const asyncId = executionAsyncId(); assert.strictEqual(asyncResource.asyncId(), fn2()); assert.notStrictEqual(asyncId, fn2()); -}); +})); const foo = {}; const fn3 = asyncResource.bind(common.mustCall(function() { diff --git a/test/parallel/test-blob-file-backed.js b/test/parallel/test-blob-file-backed.js index 6e919d2982f78d..f94eae6d6ed760 100644 --- a/test/parallel/test-blob-file-backed.js +++ b/test/parallel/test-blob-file-backed.js @@ -1,11 +1,7 @@ 'use strict'; const common = require('../common'); -const { - strictEqual, - rejects, - throws, -} = require('assert'); +const assert = require('assert'); const { TextDecoder } = require('util'); const { writeFileSync, @@ -39,22 +35,22 @@ writeFileSync(testfile5, ''); const ab = await blob.arrayBuffer(); const dec = new TextDecoder(); - strictEqual(dec.decode(new Uint8Array(ab)), data); - strictEqual(await blob.text(), data); + assert.strictEqual(dec.decode(new Uint8Array(ab)), data); + assert.strictEqual(await blob.text(), data); // Can be read multiple times let stream = blob.stream(); let check = ''; for await (const chunk of stream) check = dec.decode(chunk); - strictEqual(check, data); + assert.strictEqual(check, data); // Can be combined with other Blob's and read const combined = new Blob(['hello', blob, 'world']); const ab2 = await combined.arrayBuffer(); - strictEqual(dec.decode(ab2.slice(0, 5)), 'hello'); - strictEqual(dec.decode(ab2.slice(5, -5)), data); - strictEqual(dec.decode(ab2.slice(-5)), 'world'); + assert.strictEqual(dec.decode(ab2.slice(0, 5)), 'hello'); + assert.strictEqual(dec.decode(ab2.slice(5, -5)), data); + assert.strictEqual(dec.decode(ab2.slice(-5)), 'world'); // If the file is modified tho, the stream errors. writeFileSync(testfile, data + 'abc'); @@ -66,7 +62,7 @@ writeFileSync(testfile5, ''); for await (const _ of stream) {} }; - await rejects(read(), { name: 'NotReadableError' }); + await assert.rejects(read(), { name: 'NotReadableError' }); await unlink(testfile); })().then(common.mustCall()); @@ -76,16 +72,16 @@ writeFileSync(testfile5, ''); const blob = await openAsBlob(testfile2); const res = blob.slice(10, 20); const ab = await res.arrayBuffer(); - strictEqual(res.size, ab.byteLength); + assert.strictEqual(res.size, ab.byteLength); let length = 0; const stream = await res.stream(); for await (const chunk of stream) length += chunk.length; - strictEqual(res.size, length); + assert.strictEqual(res.size, length); const res1 = blob.slice(995, 1005); - strictEqual(await res1.text(), data.slice(995, 1005)); + assert.strictEqual(await res1.text(), data.slice(995, 1005)); // Refs: https://github.com/nodejs/node/issues/53908 for (const res2 of [ @@ -93,7 +89,7 @@ writeFileSync(testfile5, ''); blob.slice(995).slice(0, 10), blob.slice(0, 1005).slice(995), ]) { - strictEqual(await res2.text(), data.slice(995, 1005)); + assert.strictEqual(await res2.text(), data.slice(995, 1005)); } await unlink(testfile2); @@ -109,27 +105,27 @@ writeFileSync(testfile5, ''); } }; - await rejects(read(), { name: 'NotReadableError' }); + await assert.rejects(read(), { name: 'NotReadableError' }); await unlink(testfile3); })().then(common.mustCall()); (async () => { const blob = await openAsBlob(testfile4); - strictEqual(blob.size, 0); - strictEqual(await blob.text(), ''); + assert.strictEqual(blob.size, 0); + assert.strictEqual(await blob.text(), ''); writeFileSync(testfile4, 'abc'); - await rejects(blob.text(), { name: 'NotReadableError' }); + await assert.rejects(blob.text(), { name: 'NotReadableError' }); await unlink(testfile4); })().then(common.mustCall()); (async () => { const blob = await openAsBlob(testfile5); - strictEqual(blob.size, 0); + assert.strictEqual(blob.size, 0); writeFileSync(testfile5, 'abc'); const stream = blob.stream(); const reader = stream.getReader(); - await rejects(() => reader.read(), { name: 'NotReadableError' }); + await assert.rejects(() => reader.read(), { name: 'NotReadableError' }); await unlink(testfile5); })().then(common.mustCall()); @@ -138,7 +134,7 @@ writeFileSync(testfile5, ''); // across worker threads. This is largely because the underlying FdEntry // is bound to the Environment/Realm under which is was created. const blob = await openAsBlob(__filename); - throws(() => structuredClone(blob), { + assert.throws(() => structuredClone(blob), { code: 'ERR_INVALID_STATE', message: 'Invalid state: File-backed Blobs are not cloneable' }); diff --git a/test/parallel/test-blob.js b/test/parallel/test-blob.js index df753c4b2975ba..8ce1c53012ddcc 100644 --- a/test/parallel/test-blob.js +++ b/test/parallel/test-blob.js @@ -336,11 +336,11 @@ assert.throws(() => new Blob({}), { const { value, done } = await reader.read(); assert.strictEqual(value.byteLength, 5); assert(!done); - setTimeout(() => { + setTimeout(common.mustCall(() => { // The blob stream is now a byte stream hence after the first read, // it should pull in the next 'hello' which is 5 bytes hence -5. assert.strictEqual(stream[kState].controller.desiredSize, 0); - }, 0); + }), 0); })().then(common.mustCall()); (async () => { @@ -365,9 +365,9 @@ assert.throws(() => new Blob({}), { const { value, done } = await reader.read(new Uint8Array(100)); assert.strictEqual(value.byteLength, 5); assert(!done); - setTimeout(() => { + setTimeout(common.mustCall(() => { assert.strictEqual(stream[kState].controller.desiredSize, 0); - }, 0); + }), 0); })().then(common.mustCall()); (async () => { @@ -378,9 +378,9 @@ assert.throws(() => new Blob({}), { const { value, done } = await reader.read(new Uint8Array(2)); assert.strictEqual(value.byteLength, 2); assert(!done); - setTimeout(() => { + setTimeout(common.mustCall(() => { assert.strictEqual(stream[kState].controller.desiredSize, -3); - }, 0); + }), 0); })().then(common.mustCall()); { diff --git a/test/parallel/test-blocklist-clone.js b/test/parallel/test-blocklist-clone.js index 4264b36f54ca1e..ea6471c7df8742 100644 --- a/test/parallel/test-blocklist-clone.js +++ b/test/parallel/test-blocklist-clone.js @@ -6,10 +6,7 @@ const { BlockList, } = require('net'); -const { - ok, - notStrictEqual, -} = require('assert'); +const assert = require('assert'); const blocklist = new BlockList(); blocklist.addAddress('123.123.123.123'); @@ -17,13 +14,13 @@ blocklist.addAddress('123.123.123.123'); const mc = new MessageChannel(); mc.port1.onmessage = common.mustCall(({ data }) => { - notStrictEqual(data, blocklist); - ok(data.check('123.123.123.123')); - ok(!data.check('123.123.123.124')); + assert.notStrictEqual(data, blocklist); + assert.ok(data.check('123.123.123.123')); + assert.ok(!data.check('123.123.123.124')); data.addAddress('123.123.123.124'); - ok(blocklist.check('123.123.123.124')); - ok(data.check('123.123.123.124')); + assert.ok(blocklist.check('123.123.123.124')); + assert.ok(data.check('123.123.123.124')); mc.port1.close(); }); diff --git a/test/parallel/test-btoa-atob.js b/test/parallel/test-btoa-atob.js index abf05adeef1042..a2c8d9e3134c99 100644 --- a/test/parallel/test-btoa-atob.js +++ b/test/parallel/test-btoa-atob.js @@ -2,37 +2,37 @@ require('../common'); -const { strictEqual, throws } = require('assert'); +const assert = require('assert'); const buffer = require('buffer'); // Exported on the global object -strictEqual(globalThis.atob, buffer.atob); -strictEqual(globalThis.btoa, buffer.btoa); +assert.strictEqual(globalThis.atob, buffer.atob); +assert.strictEqual(globalThis.btoa, buffer.btoa); // Throws type error on no argument passed -throws(() => buffer.atob(), /TypeError/); -throws(() => buffer.btoa(), /TypeError/); +assert.throws(() => buffer.atob(), /TypeError/); +assert.throws(() => buffer.btoa(), /TypeError/); -strictEqual(atob(' '), ''); -strictEqual(atob(' Y\fW\tJ\njZ A=\r= '), 'abcd'); +assert.strictEqual(atob(' '), ''); +assert.strictEqual(atob(' Y\fW\tJ\njZ A=\r= '), 'abcd'); -strictEqual(atob(null), '\x9Eée'); -strictEqual(atob(NaN), '5£'); -strictEqual(atob(Infinity), '"wâ\x9E+r'); -strictEqual(atob(true), '¶»\x9E'); -strictEqual(atob(1234), '×mø'); -strictEqual(atob([]), ''); -strictEqual(atob({ toString: () => '' }), ''); -strictEqual(atob({ [Symbol.toPrimitive]: () => '' }), ''); +assert.strictEqual(atob(null), '\x9Eée'); +assert.strictEqual(atob(NaN), '5£'); +assert.strictEqual(atob(Infinity), '"wâ\x9E+r'); +assert.strictEqual(atob(true), '¶»\x9E'); +assert.strictEqual(atob(1234), '×mø'); +assert.strictEqual(atob([]), ''); +assert.strictEqual(atob({ toString: () => '' }), ''); +assert.strictEqual(atob({ [Symbol.toPrimitive]: () => '' }), ''); -throws(() => atob(Symbol()), /TypeError/); +assert.throws(() => atob(Symbol()), /TypeError/); [ 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 - throws(() => atob(value), { + assert.throws(() => atob(value), { constructor: DOMException, name: 'InvalidCharacterError', code: 5, diff --git a/test/parallel/test-buffer-bytelength.js b/test/parallel/test-buffer-bytelength.js index 1013469181fa23..fec508ffb4048a 100644 --- a/test/parallel/test-buffer-bytelength.js +++ b/test/parallel/test-buffer-bytelength.js @@ -105,17 +105,13 @@ assert.strictEqual(Buffer.byteLength('aaaa==', 'base64url'), 3); assert.strictEqual(Buffer.byteLength('Il était tué'), 14); assert.strictEqual(Buffer.byteLength('Il était tué', 'utf8'), 14); -['ascii', 'latin1', 'binary'] - .reduce((es, e) => es.concat(e, e.toUpperCase()), []) - .forEach((encoding) => { - assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 12); - }); - -['ucs2', 'ucs-2', 'utf16le', 'utf-16le'] - .reduce((es, e) => es.concat(e, e.toUpperCase()), []) - .forEach((encoding) => { - assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 24); - }); +for (const encoding of ['ascii', 'latin1', 'binary'].flatMap((e) => [e, e.toUpperCase()])) { + assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 12); +} + +for (const encoding of ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].flatMap((e) => [e, e.toUpperCase()])) { + assert.strictEqual(Buffer.byteLength('Il était tué', encoding), 24); +} // Test that ArrayBuffer from a different context is detected correctly const arrayBuf = vm.runInNewContext('new ArrayBuffer()'); diff --git a/test/parallel/test-buffer-from.js b/test/parallel/test-buffer-from.js index 416a3b3a3105b5..6c08b973e6ab41 100644 --- a/test/parallel/test-buffer-from.js +++ b/test/parallel/test-buffer-from.js @@ -1,7 +1,7 @@ 'use strict'; const common = require('../common'); -const { deepStrictEqual, strictEqual, throws } = require('assert'); +const assert = require('assert'); const { runInNewContext } = require('vm'); const checkString = 'test'; @@ -26,10 +26,10 @@ class MyBadPrimitive { } } -deepStrictEqual(Buffer.from(new String(checkString)), check); -deepStrictEqual(Buffer.from(new MyString()), check); -deepStrictEqual(Buffer.from(new MyPrimitive()), check); -deepStrictEqual( +assert.deepStrictEqual(Buffer.from(new String(checkString)), check); +assert.deepStrictEqual(Buffer.from(new MyString()), check); +assert.deepStrictEqual(Buffer.from(new MyPrimitive()), check); +assert.deepStrictEqual( Buffer.from(runInNewContext('new String(checkString)', { checkString })), check ); @@ -56,8 +56,8 @@ deepStrictEqual( 'Buffer, ArrayBuffer, or Array or an Array-like Object.' + common.invalidArgTypeHelper(input) }; - throws(() => Buffer.from(input), errObj); - throws(() => Buffer.from(input, 'hex'), errObj); + assert.throws(() => Buffer.from(input), errObj); + assert.throws(() => Buffer.from(input, 'hex'), errObj); }); Buffer.allocUnsafe(10); // Should not throw. @@ -68,9 +68,9 @@ Buffer.from('deadbeaf', 'hex'); // Should not throw. const u16 = new Uint16Array([0xffff]); const b16 = Buffer.copyBytesFrom(u16); u16[0] = 0; - strictEqual(b16.length, 2); - strictEqual(b16[0], 255); - strictEqual(b16[1], 255); + assert.strictEqual(b16.length, 2); + assert.strictEqual(b16[0], 255); + assert.strictEqual(b16[1], 255); } { @@ -78,30 +78,30 @@ Buffer.from('deadbeaf', 'hex'); // Should not throw. const b16 = Buffer.copyBytesFrom(u16, 1, 5); u16[0] = 0xffff; u16[1] = 0; - strictEqual(b16.length, 2); - strictEqual(b16[0], 255); - strictEqual(b16[1], 255); + assert.strictEqual(b16.length, 2); + assert.strictEqual(b16[0], 255); + assert.strictEqual(b16[1], 255); } { const u32 = new Uint32Array([0xffffffff]); const b32 = Buffer.copyBytesFrom(u32); u32[0] = 0; - strictEqual(b32.length, 4); - strictEqual(b32[0], 255); - strictEqual(b32[1], 255); - strictEqual(b32[2], 255); - strictEqual(b32[3], 255); + assert.strictEqual(b32.length, 4); + assert.strictEqual(b32[0], 255); + assert.strictEqual(b32[1], 255); + assert.strictEqual(b32[2], 255); + assert.strictEqual(b32[3], 255); } -throws(() => { +assert.throws(() => { Buffer.copyBytesFrom(); }, { code: 'ERR_INVALID_ARG_TYPE', }); ['', Symbol(), true, false, {}, [], () => {}, 1, 1n, null, undefined].forEach( - (notTypedArray) => throws(() => { + (notTypedArray) => assert.throws(() => { Buffer.copyBytesFrom('nope'); }, { code: 'ERR_INVALID_ARG_TYPE', @@ -109,7 +109,7 @@ throws(() => { ); ['', Symbol(), true, false, {}, [], () => {}, 1n].forEach((notANumber) => - throws(() => { + assert.throws(() => { Buffer.copyBytesFrom(new Uint8Array(1), notANumber); }, { code: 'ERR_INVALID_ARG_TYPE', @@ -117,7 +117,7 @@ throws(() => { ); [-1, NaN, 1.1, -Infinity].forEach((outOfRange) => - throws(() => { + assert.throws(() => { Buffer.copyBytesFrom(new Uint8Array(1), outOfRange); }, { code: 'ERR_OUT_OF_RANGE', @@ -125,7 +125,7 @@ throws(() => { ); ['', Symbol(), true, false, {}, [], () => {}, 1n].forEach((notANumber) => - throws(() => { + assert.throws(() => { Buffer.copyBytesFrom(new Uint8Array(1), 0, notANumber); }, { code: 'ERR_INVALID_ARG_TYPE', @@ -133,7 +133,7 @@ throws(() => { ); [-1, NaN, 1.1, -Infinity].forEach((outOfRange) => - throws(() => { + assert.throws(() => { Buffer.copyBytesFrom(new Uint8Array(1), 0, outOfRange); }, { code: 'ERR_OUT_OF_RANGE', diff --git a/test/parallel/test-buffer-resizable.js b/test/parallel/test-buffer-resizable.js index dcfe6385b680d8..133206c763f4ab 100644 --- a/test/parallel/test-buffer-resizable.js +++ b/test/parallel/test-buffer-resizable.js @@ -3,27 +3,27 @@ require('../common'); const { Buffer } = require('node:buffer'); -const { strictEqual } = require('node:assert'); +const assert = require('node:assert'); const { describe, it } = require('node:test'); describe('Using resizable ArrayBuffer with Buffer...', () => { it('works as expected', () => { const ab = new ArrayBuffer(10, { maxByteLength: 20 }); const buffer = Buffer.from(ab, 1); - strictEqual(buffer.byteLength, 9); + assert.strictEqual(buffer.byteLength, 9); ab.resize(15); - strictEqual(buffer.byteLength, 14); + assert.strictEqual(buffer.byteLength, 14); ab.resize(5); - strictEqual(buffer.byteLength, 4); + assert.strictEqual(buffer.byteLength, 4); }); it('works with the deprecated constructor also', () => { const ab = new ArrayBuffer(10, { maxByteLength: 20 }); const buffer = new Buffer(ab, 1); - strictEqual(buffer.byteLength, 9); + assert.strictEqual(buffer.byteLength, 9); ab.resize(15); - strictEqual(buffer.byteLength, 14); + assert.strictEqual(buffer.byteLength, 14); ab.resize(5); - strictEqual(buffer.byteLength, 4); + assert.strictEqual(buffer.byteLength, 4); }); }); diff --git a/test/parallel/test-buffer-tostring.js b/test/parallel/test-buffer-tostring.js index 4219649fa37eb3..a3dad0146d7536 100644 --- a/test/parallel/test-buffer-tostring.js +++ b/test/parallel/test-buffer-tostring.js @@ -4,14 +4,10 @@ const common = require('../common'); const assert = require('assert'); // utf8, ucs2, ascii, latin1, utf16le -const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1', - 'binary', 'utf16le', 'utf-16le']; - -encodings - .reduce((es, e) => es.concat(e, e.toUpperCase()), []) - .forEach((encoding) => { - assert.strictEqual(Buffer.from('foo', encoding).toString(encoding), 'foo'); - }); +for (const encoding of ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1', + 'binary', 'utf16le', 'utf-16le'].flatMap((e) => [e, e.toUpperCase()])) { + assert.strictEqual(Buffer.from('foo', encoding).toString(encoding), 'foo'); +} // base64 ['base64', 'BASE64'].forEach((encoding) => { diff --git a/test/parallel/test-buffer-write.js b/test/parallel/test-buffer-write.js index 81076629b1d847..4c797059b5925b 100644 --- a/test/parallel/test-buffer-write.js +++ b/test/parallel/test-buffer-write.js @@ -28,21 +28,14 @@ const resultMap = new Map([ ]); // utf8, ucs2, ascii, latin1, utf16le -const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1', - 'binary', 'utf16le', 'utf-16le']; - -encodings - .reduce((es, e) => es.concat(e, e.toUpperCase()), []) - .forEach((encoding) => { - const buf = Buffer.alloc(9); - const len = Buffer.byteLength('foo', encoding); - assert.strictEqual(buf.write('foo', 0, len, encoding), len); - - if (encoding.includes('-')) - encoding = encoding.replace('-', ''); +for (const encoding of ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1', + 'binary', 'utf16le', 'utf-16le'].flatMap((e) => [e, e.toUpperCase()])) { + const buf = Buffer.alloc(9); + const len = Buffer.byteLength('foo', encoding); + assert.strictEqual(buf.write('foo', 0, len, encoding), len); - assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase())); - }); + assert.deepStrictEqual(buf, resultMap.get(encoding.replace('-', '').toLowerCase())); +} // base64 ['base64', 'BASE64', 'base64url', 'BASE64URL'].forEach((encoding) => {