diff --git a/lib/assert.js b/lib/assert.js index 3e212a1c3aebbe..5161f3c34f2321 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -50,6 +50,7 @@ const { StringPrototypeSlice, StringPrototypeSplit, SymbolIterator, + Uint8Array, } = primordials; const { @@ -73,6 +74,9 @@ const { isDate, isWeakSet, isWeakMap, + isSharedArrayBuffer, + isArrayBuffer, + isInt16Array, } = require('internal/util/types'); const { isError, deprecate, emitExperimentalWarning } = require('internal/util'); const { innerOk } = require('internal/assert/utils'); @@ -369,7 +373,7 @@ function isSpecial(obj) { } const typesToCallDeepStrictEqualWith = [ - isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, + isKeyObject, isWeakSet, isWeakMap, Buffer.isBuffer, isSharedArrayBuffer, isInt16Array, ]; /** @@ -406,6 +410,22 @@ function compareBranch( return true; } + if (isArrayBuffer(actual) && isArrayBuffer(expected)) { + if (actual.byteLength !== expected.byteLength) { + return false; + } + + const actualView = new Uint8Array(actual); + const expectedView = new Uint8Array(expected); + + for (let i = 0; i < actualView.length; i++) { + if (actualView[i] !== expectedView[i]) { + return false; + } + } + return true; + } + for (const type of typesToCallDeepStrictEqualWith) { if (type(actual) || type(expected)) { if (isDeepStrictEqual === undefined) lazyLoadComparison(); diff --git a/test/parallel/test-assert-objects.js b/test/parallel/test-assert-objects.js index d1c8bb854babb0..28bccb1117b378 100644 --- a/test/parallel/test-assert-objects.js +++ b/test/parallel/test-assert-objects.js @@ -207,6 +207,16 @@ describe('Object Comparison Tests', () => { actual: [1, 2, 3], expected: ['2'], }, + { + description: 'throws when comparing a ArrayBuffer with a SharedArrayBuffer', + actual: new ArrayBuffer(3), + expected: new SharedArrayBuffer(3), + }, + { + description: 'throws when comparing an Int16Array with a Uint16Array', + actual: new Int16Array(3), + expected: new Uint16Array(3), + }, ]; if (common.hasCrypto) { @@ -347,6 +357,11 @@ describe('Object Comparison Tests', () => { actual: { typedArray: new Uint8Array([1, 2, 3]) }, expected: { typedArray: new Uint8Array([1, 2, 3]) }, }, + { + description: 'compares two objects with TypedArray instances with the same content', + actual: { typedArray: new Int16Array([1, 2, 3]) }, + expected: { typedArray: new Int16Array([1, 2, 3]) }, + }, { description: 'compares two Map objects with identical entries', actual: new Map([ diff --git a/test/parallel/test-assert-typedarray-deepequal.js b/test/parallel/test-assert-typedarray-deepequal.js index 1c1c4c030a267e..243b51de1cd4a3 100644 --- a/test/parallel/test-assert-typedarray-deepequal.js +++ b/test/parallel/test-assert-typedarray-deepequal.js @@ -99,6 +99,10 @@ suite('notEqualArrayPairs', () => { makeBlock(assert.deepStrictEqual, arrayPair[0], arrayPair[1]), assert.AssertionError ); + assert.throws( + makeBlock(assert.partialDeepStrictEqual, arrayPair[0], arrayPair[1]), + assert.AssertionError + ); }); } });