|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const vm = require('node:vm'); |
| 5 | +const assert = require('node:assert'); |
| 6 | +const { describe, it } = require('node:test'); |
| 7 | + |
| 8 | +function createCircularObject() { |
| 9 | + const obj = {}; |
| 10 | + obj.self = obj; |
| 11 | + return obj; |
| 12 | +} |
| 13 | + |
| 14 | +function createDeepNestedObject() { |
| 15 | + return { level1: { level2: { level3: 'deepValue' } } }; |
| 16 | +} |
| 17 | + |
| 18 | +async function generateCryptoKey() { |
| 19 | + const { KeyObject } = require('node:crypto'); |
| 20 | + const { subtle } = globalThis.crypto; |
| 21 | + |
| 22 | + const cryptoKey = await subtle.generateKey( |
| 23 | + { |
| 24 | + name: 'HMAC', |
| 25 | + hash: 'SHA-256', |
| 26 | + length: 256, |
| 27 | + }, |
| 28 | + true, |
| 29 | + ['sign', 'verify'] |
| 30 | + ); |
| 31 | + |
| 32 | + const keyObject = KeyObject.from(cryptoKey); |
| 33 | + |
| 34 | + return { cryptoKey, keyObject }; |
| 35 | +} |
| 36 | + |
| 37 | +describe('Object Comparison Tests', () => { |
| 38 | + describe('partialDeepStrictEqual', () => { |
| 39 | + describe('throws an error', () => { |
| 40 | + const tests = [ |
| 41 | + { |
| 42 | + description: 'throws when only one argument is provided', |
| 43 | + actual: { a: 1 }, |
| 44 | + expected: undefined, |
| 45 | + }, |
| 46 | + { |
| 47 | + description: 'throws when expected has more properties than actual', |
| 48 | + actual: [1, 'two'], |
| 49 | + expected: [1, 'two', true], |
| 50 | + }, |
| 51 | + { |
| 52 | + description: 'throws because expected has seven 2 while actual has six one', |
| 53 | + actual: [1, 2, 2, 2, 2, 2, 2, 3], |
| 54 | + expected: [1, 2, 2, 2, 2, 2, 2, 2], |
| 55 | + }, |
| 56 | + { |
| 57 | + description: 'throws when comparing two different sets with objects', |
| 58 | + actual: new Set([{ a: 1 }]), |
| 59 | + expected: new Set([{ a: 1 }, { b: 1 }]), |
| 60 | + }, |
| 61 | + |
| 62 | + { |
| 63 | + description: 'throws when comparing two different objects', |
| 64 | + actual: { a: 1, b: 'string' }, |
| 65 | + expected: { a: 2, b: 'string' }, |
| 66 | + }, |
| 67 | + { |
| 68 | + description: |
| 69 | + 'throws when comparing two objects with different nested objects', |
| 70 | + actual: createDeepNestedObject(), |
| 71 | + expected: { level1: { level2: { level3: 'differentValue' } } }, |
| 72 | + }, |
| 73 | + { |
| 74 | + description: |
| 75 | + 'throws when comparing two objects with different RegExp properties', |
| 76 | + actual: { pattern: /abc/ }, |
| 77 | + expected: { pattern: /def/ }, |
| 78 | + }, |
| 79 | + { |
| 80 | + description: |
| 81 | + 'throws when comparing two arrays with different elements', |
| 82 | + actual: [1, 'two', true], |
| 83 | + expected: [1, 'two', false], |
| 84 | + }, |
| 85 | + { |
| 86 | + description: |
| 87 | + 'throws when comparing two Date objects with different times', |
| 88 | + actual: new Date(0), |
| 89 | + expected: new Date(1), |
| 90 | + }, |
| 91 | + { |
| 92 | + description: |
| 93 | + 'throws when comparing two objects with different large number of properties', |
| 94 | + actual: Object.fromEntries( |
| 95 | + Array.from({ length: 100 }, (_, i) => [`key${i}`, i]) |
| 96 | + ), |
| 97 | + expected: Object.fromEntries( |
| 98 | + Array.from({ length: 100 }, (_, i) => [`key${i}`, i + 1]) |
| 99 | + ), |
| 100 | + }, |
| 101 | + { |
| 102 | + description: |
| 103 | + 'throws when comparing two objects with different Symbols', |
| 104 | + actual: { [Symbol('test')]: 'symbol' }, |
| 105 | + expected: { [Symbol('test')]: 'symbol' }, |
| 106 | + }, |
| 107 | + { |
| 108 | + description: |
| 109 | + 'throws when comparing two objects with different array properties', |
| 110 | + actual: { a: [1, 2, 3] }, |
| 111 | + expected: { a: [1, 2, 4] }, |
| 112 | + }, |
| 113 | + { |
| 114 | + description: |
| 115 | + 'throws when comparing two objects with different function properties', |
| 116 | + actual: { fn: () => {} }, |
| 117 | + expected: { fn: () => {} }, |
| 118 | + }, |
| 119 | + { |
| 120 | + description: |
| 121 | + 'throws when comparing two objects with different Error instances', |
| 122 | + actual: { error: new Error('Test error 1') }, |
| 123 | + expected: { error: new Error('Test error 2') }, |
| 124 | + }, |
| 125 | + { |
| 126 | + description: |
| 127 | + 'throws when comparing two objects with different TypedArray instances and content', |
| 128 | + actual: { typedArray: new Uint8Array([1, 2, 3]) }, |
| 129 | + expected: { typedArray: new Uint8Array([4, 5, 6]) }, |
| 130 | + }, |
| 131 | + { |
| 132 | + description: |
| 133 | + 'throws when comparing two Map objects with different entries', |
| 134 | + actual: new Map([ |
| 135 | + ['key1', 'value1'], |
| 136 | + ['key2', 'value2'], |
| 137 | + ]), |
| 138 | + expected: new Map([ |
| 139 | + ['key1', 'value1'], |
| 140 | + ['key3', 'value3'], |
| 141 | + ]), |
| 142 | + }, |
| 143 | + { |
| 144 | + description: |
| 145 | + 'throws when comparing two Map objects with different keys', |
| 146 | + actual: new Map([ |
| 147 | + ['key1', 'value1'], |
| 148 | + ['key2', 'value2'], |
| 149 | + ]), |
| 150 | + expected: new Map([ |
| 151 | + ['key1', 'value1'], |
| 152 | + ['key3', 'value2'], |
| 153 | + ]), |
| 154 | + }, |
| 155 | + { |
| 156 | + description: |
| 157 | + 'throws when comparing two Map objects with different length', |
| 158 | + actual: new Map([ |
| 159 | + ['key1', 'value1'], |
| 160 | + ['key2', 'value2'], |
| 161 | + ]), |
| 162 | + expected: new Map([['key1', 'value1']]), |
| 163 | + }, |
| 164 | + { |
| 165 | + description: |
| 166 | + 'throws when comparing two TypedArray instances with different content', |
| 167 | + actual: new Uint8Array(10), |
| 168 | + expected: () => { |
| 169 | + const typedArray2 = new Int8Array(10); |
| 170 | + Object.defineProperty(typedArray2, Symbol.toStringTag, { |
| 171 | + value: 'Uint8Array' |
| 172 | + }); |
| 173 | + Object.setPrototypeOf(typedArray2, Uint8Array.prototype); |
| 174 | + |
| 175 | + return typedArray2; |
| 176 | + }, |
| 177 | + }, |
| 178 | + { |
| 179 | + description: |
| 180 | + 'throws when comparing two Set objects from different realms with different values', |
| 181 | + actual: new vm.runInNewContext('new Set(["value1", "value2"])'), |
| 182 | + expected: new Set(['value1', 'value3']), |
| 183 | + }, |
| 184 | + { |
| 185 | + description: |
| 186 | + 'throws when comparing two Set objects with different values', |
| 187 | + actual: new Set(['value1', 'value2']), |
| 188 | + expected: new Set(['value1', 'value3']), |
| 189 | + }, |
| 190 | + { |
| 191 | + description: 'throws when comparing one subset object with another', |
| 192 | + actual: { a: 1, b: 2, c: 3 }, |
| 193 | + expected: { b: '2' }, |
| 194 | + }, |
| 195 | + { |
| 196 | + description: 'throws when comparing one subset array with another', |
| 197 | + actual: [1, 2, 3], |
| 198 | + expected: ['2'], |
| 199 | + }, |
| 200 | + ]; |
| 201 | + |
| 202 | + if (common.hasCrypto) { |
| 203 | + tests.push({ |
| 204 | + description: |
| 205 | + 'throws when comparing two objects with different CryptoKey instances objects', |
| 206 | + actual: async () => { |
| 207 | + return generateCryptoKey(); |
| 208 | + }, |
| 209 | + expected: async () => { |
| 210 | + return generateCryptoKey(); |
| 211 | + }, |
| 212 | + }); |
| 213 | + |
| 214 | + const { createSecretKey } = require('node:crypto'); |
| 215 | + |
| 216 | + tests.push({ |
| 217 | + description: |
| 218 | + 'throws when comparing two objects with different KeyObject instances objects', |
| 219 | + actual: createSecretKey(Buffer.alloc(1, 0)), |
| 220 | + expected: createSecretKey(Buffer.alloc(1, 1)), |
| 221 | + }); |
| 222 | + } |
| 223 | + |
| 224 | + tests.forEach(({ description, actual, expected }) => { |
| 225 | + it(description, () => { |
| 226 | + assert.throws(() => assert.partialDeepStrictEqual(actual, expected), Error); |
| 227 | + }); |
| 228 | + }); |
| 229 | + }); |
| 230 | + }); |
| 231 | + |
| 232 | + describe('does not throw an error', () => { |
| 233 | + const sym = Symbol('test'); |
| 234 | + const func = () => {}; |
| 235 | + |
| 236 | + [ |
| 237 | + { |
| 238 | + description: 'compares two identical simple objects', |
| 239 | + actual: { a: 1, b: 'string' }, |
| 240 | + expected: { a: 1, b: 'string' }, |
| 241 | + }, |
| 242 | + { |
| 243 | + description: 'compares two objects with different property order', |
| 244 | + actual: { a: 1, b: 'string' }, |
| 245 | + expected: { b: 'string', a: 1 }, |
| 246 | + }, |
| 247 | + { |
| 248 | + description: 'compares two deeply nested objects with partial equality', |
| 249 | + actual: { a: { nested: { property: true, some: 'other' } } }, |
| 250 | + expected: { a: { nested: { property: true } } }, |
| 251 | + }, |
| 252 | + { |
| 253 | + description: |
| 254 | + 'compares plain objects from different realms', |
| 255 | + actual: vm.runInNewContext(`({ |
| 256 | + a: 1, |
| 257 | + b: 2n, |
| 258 | + c: "3", |
| 259 | + d: /4/, |
| 260 | + e: new Set([5]), |
| 261 | + f: [6], |
| 262 | + g: new Uint8Array() |
| 263 | + })`), |
| 264 | + expected: { b: 2n, e: new Set([5]), f: [6], g: new Uint8Array() }, |
| 265 | + }, |
| 266 | + { |
| 267 | + description: 'compares two integers', |
| 268 | + actual: 1, |
| 269 | + expected: 1, |
| 270 | + }, |
| 271 | + { |
| 272 | + description: 'compares two strings', |
| 273 | + actual: '1', |
| 274 | + expected: '1', |
| 275 | + }, |
| 276 | + { |
| 277 | + description: 'compares two objects with nested objects', |
| 278 | + actual: createDeepNestedObject(), |
| 279 | + expected: createDeepNestedObject(), |
| 280 | + }, |
| 281 | + { |
| 282 | + description: 'compares two objects with circular references', |
| 283 | + actual: createCircularObject(), |
| 284 | + expected: createCircularObject(), |
| 285 | + }, |
| 286 | + { |
| 287 | + description: 'compares two arrays with identical elements', |
| 288 | + actual: [1, 'two', true], |
| 289 | + expected: [1, 'two', true], |
| 290 | + }, |
| 291 | + { |
| 292 | + description: 'compares two Date objects with the same time', |
| 293 | + actual: new Date(0), |
| 294 | + expected: new Date(0), |
| 295 | + }, |
| 296 | + { |
| 297 | + description: 'compares two objects with large number of properties', |
| 298 | + actual: Object.fromEntries( |
| 299 | + Array.from({ length: 100 }, (_, i) => [`key${i}`, i]) |
| 300 | + ), |
| 301 | + expected: Object.fromEntries( |
| 302 | + Array.from({ length: 100 }, (_, i) => [`key${i}`, i]) |
| 303 | + ), |
| 304 | + }, |
| 305 | + { |
| 306 | + description: 'compares two objects with Symbol properties', |
| 307 | + actual: { [sym]: 'symbol' }, |
| 308 | + expected: { [sym]: 'symbol' }, |
| 309 | + }, |
| 310 | + { |
| 311 | + description: 'compares two objects with RegExp properties', |
| 312 | + actual: { pattern: /abc/ }, |
| 313 | + expected: { pattern: /abc/ }, |
| 314 | + }, |
| 315 | + { |
| 316 | + description: 'compares two objects with identical function properties', |
| 317 | + actual: { fn: func }, |
| 318 | + expected: { fn: func }, |
| 319 | + }, |
| 320 | + { |
| 321 | + description: 'compares two objects with mixed types of properties', |
| 322 | + actual: { num: 1, str: 'test', bool: true, sym }, |
| 323 | + expected: { num: 1, str: 'test', bool: true, sym }, |
| 324 | + }, |
| 325 | + { |
| 326 | + description: 'compares two objects with Buffers', |
| 327 | + actual: { buf: Buffer.from('Node.js') }, |
| 328 | + expected: { buf: Buffer.from('Node.js') }, |
| 329 | + }, |
| 330 | + { |
| 331 | + description: 'compares two objects with identical Error properties', |
| 332 | + actual: { error: new Error('Test error') }, |
| 333 | + expected: { error: new Error('Test error') }, |
| 334 | + }, |
| 335 | + { |
| 336 | + description: 'compares two objects with TypedArray instances with the same content', |
| 337 | + actual: { typedArray: new Uint8Array([1, 2, 3]) }, |
| 338 | + expected: { typedArray: new Uint8Array([1, 2, 3]) }, |
| 339 | + }, |
| 340 | + { |
| 341 | + description: 'compares two Map objects with identical entries', |
| 342 | + actual: new Map([ |
| 343 | + ['key1', 'value1'], |
| 344 | + ['key2', 'value2'], |
| 345 | + ]), |
| 346 | + expected: new Map([ |
| 347 | + ['key1', 'value1'], |
| 348 | + ['key2', 'value2'], |
| 349 | + ]), |
| 350 | + }, |
| 351 | + { |
| 352 | + describe: 'compares two array of objects', |
| 353 | + actual: [{ a: 5 }], |
| 354 | + expected: [{ a: 5 }], |
| 355 | + }, |
| 356 | + { |
| 357 | + describe: 'compares two array of objects where expected is a subset of actual', |
| 358 | + actual: [{ a: 5 }, { b: 5 }], |
| 359 | + expected: [{ a: 5 }], |
| 360 | + }, |
| 361 | + { |
| 362 | + description: 'compares two Set objects with identical objects', |
| 363 | + actual: new Set([{ a: 1 }]), |
| 364 | + expected: new Set([{ a: 1 }]), |
| 365 | + }, |
| 366 | + { |
| 367 | + description: 'compares two Set objects where expected is a subset of actual', |
| 368 | + actual: new Set([{ a: 1 }, { b: 1 }]), |
| 369 | + expected: new Set([{ a: 1 }]), |
| 370 | + }, |
| 371 | + { |
| 372 | + description: 'compares two Set objects with identical arrays', |
| 373 | + actual: new Set(['value1', 'value2']), |
| 374 | + expected: new Set(['value1', 'value2']), |
| 375 | + }, |
| 376 | + { |
| 377 | + description: 'compares two Set objects', |
| 378 | + actual: new Set(['value1', 'value2', 'value3']), |
| 379 | + expected: new Set(['value1', 'value2']), |
| 380 | + }, |
| 381 | + { |
| 382 | + description: |
| 383 | + 'compares two Map objects from different realms with identical entries', |
| 384 | + actual: new vm.runInNewContext( |
| 385 | + 'new Map([["key1", "value1"], ["key2", "value2"]])' |
| 386 | + ), |
| 387 | + expected: new Map([ |
| 388 | + ['key1', 'value1'], |
| 389 | + ['key2', 'value2'], |
| 390 | + ]), |
| 391 | + }, |
| 392 | + { |
| 393 | + description: |
| 394 | + 'compares two objects with identical getter/setter properties', |
| 395 | + actual: (() => { |
| 396 | + let value = 'test'; |
| 397 | + return Object.defineProperty({}, 'prop', { |
| 398 | + get: () => value, |
| 399 | + set: (newValue) => { |
| 400 | + value = newValue; |
| 401 | + }, |
| 402 | + enumerable: true, |
| 403 | + configurable: true, |
| 404 | + }); |
| 405 | + })(), |
| 406 | + expected: (() => { |
| 407 | + let value = 'test'; |
| 408 | + return Object.defineProperty({}, 'prop', { |
| 409 | + get: () => value, |
| 410 | + set: (newValue) => { |
| 411 | + value = newValue; |
| 412 | + }, |
| 413 | + enumerable: true, |
| 414 | + configurable: true, |
| 415 | + }); |
| 416 | + })(), |
| 417 | + }, |
| 418 | + { |
| 419 | + description: 'compares two objects with no prototype', |
| 420 | + actual: { __proto__: null, prop: 'value' }, |
| 421 | + expected: { __proto__: null, prop: 'value' }, |
| 422 | + }, |
| 423 | + { |
| 424 | + description: |
| 425 | + 'compares two objects with identical non-enumerable properties', |
| 426 | + actual: (() => { |
| 427 | + const obj = {}; |
| 428 | + Object.defineProperty(obj, 'hidden', { |
| 429 | + value: 'secret', |
| 430 | + enumerable: false, |
| 431 | + }); |
| 432 | + return obj; |
| 433 | + })(), |
| 434 | + expected: (() => { |
| 435 | + const obj = {}; |
| 436 | + Object.defineProperty(obj, 'hidden', { |
| 437 | + value: 'secret', |
| 438 | + enumerable: false, |
| 439 | + }); |
| 440 | + return obj; |
| 441 | + })(), |
| 442 | + }, |
| 443 | + { |
| 444 | + description: 'compares two identical primitives, string', |
| 445 | + actual: 'foo', |
| 446 | + expected: 'foo', |
| 447 | + }, |
| 448 | + { |
| 449 | + description: 'compares two identical primitives, number', |
| 450 | + actual: 1, |
| 451 | + expected: 1, |
| 452 | + }, |
| 453 | + { |
| 454 | + description: 'compares two identical primitives, boolean', |
| 455 | + actual: false, |
| 456 | + expected: false, |
| 457 | + }, |
| 458 | + { |
| 459 | + description: 'compares two identical primitives, null', |
| 460 | + actual: null, |
| 461 | + expected: null, |
| 462 | + }, |
| 463 | + { |
| 464 | + description: 'compares two identical primitives, undefined', |
| 465 | + actual: undefined, |
| 466 | + expected: undefined, |
| 467 | + }, |
| 468 | + { |
| 469 | + description: 'compares two identical primitives, Symbol', |
| 470 | + actual: sym, |
| 471 | + expected: sym, |
| 472 | + }, |
| 473 | + { |
| 474 | + description: |
| 475 | + 'compares one subset object with another', |
| 476 | + actual: { a: 1, b: 2, c: 3 }, |
| 477 | + expected: { b: 2 }, |
| 478 | + }, |
| 479 | + { |
| 480 | + description: |
| 481 | + 'compares one subset array with another', |
| 482 | + actual: [1, 2, 3], |
| 483 | + expected: [2], |
| 484 | + }, |
| 485 | + ].forEach(({ description, actual, expected }) => { |
| 486 | + it(description, () => { |
| 487 | + assert.partialDeepStrictEqual(actual, expected); |
| 488 | + }); |
| 489 | + }); |
| 490 | + }); |
| 491 | +}); |
0 commit comments