|
1 |
| -import { describe, expect, it } from 'vitest' |
| 1 | +import { afterEach, describe, expect, it } from 'vitest' |
2 | 2 | import { deepEqual, isPlainArray, replaceEqualDeep } from '../src/utils'
|
3 | 3 |
|
4 | 4 | describe('replaceEqualDeep', () => {
|
@@ -434,4 +434,49 @@ describe('deepEqual', () => {
|
434 | 434 | expect(deepEqual(b, a, { partial: true })).toEqual(false)
|
435 | 435 | })
|
436 | 436 | })
|
| 437 | + |
| 438 | + // This might not be what we want, but this test documents how things are now |
| 439 | + describe('symbol and non-enumerable properties are not handled', () => { |
| 440 | + it.fails( |
| 441 | + 'should return `false` for unequal objects with symbol properties', |
| 442 | + () => { |
| 443 | + const key = Symbol('foo') |
| 444 | + const a = { [key]: 1 } |
| 445 | + const b = { [key]: 2 } |
| 446 | + expect(deepEqual(a, b)).toEqual(false) |
| 447 | + }, |
| 448 | + ) |
| 449 | + |
| 450 | + it.fails( |
| 451 | + 'should return `false` for unequal objects with non-enumerable properties', |
| 452 | + () => { |
| 453 | + const a = {} |
| 454 | + Object.defineProperty(a, 'prop', { value: 1, enumerable: false }) |
| 455 | + const b = {} |
| 456 | + Object.defineProperty(b, 'prop', { value: 2, enumerable: false }) |
| 457 | + expect(deepEqual(a, b)).toEqual(false) |
| 458 | + }, |
| 459 | + ) |
| 460 | + }) |
| 461 | + |
| 462 | + // We voluntarily fail in this case, because users should not do it, and ignoring it enables some performance improvements |
| 463 | + describe('augmented object prototype fail case (no one should do this anyway)', () => { |
| 464 | + it.fails( |
| 465 | + 'should not compare objects with augmented prototype properties', |
| 466 | + () => { |
| 467 | + // @ts-expect-error -- typescript is right to complain here, don't do this! |
| 468 | + Object.prototype.x = 'x' |
| 469 | + const a = { a: 1 } |
| 470 | + const b = { a: 1 } |
| 471 | + expect(deepEqual(a, b, { ignoreUndefined: false })).toEqual(true) |
| 472 | + }, |
| 473 | + ) |
| 474 | + |
| 475 | + afterEach(() => { |
| 476 | + // it's probably not necessary to clean this up because vitest isolates tests |
| 477 | + // but just in case isolation ever gets disabled, we clean the prototype to avoid disturbing other tests |
| 478 | + // @ts-expect-error |
| 479 | + delete Object.prototype.x |
| 480 | + }) |
| 481 | + }) |
437 | 482 | })
|
0 commit comments