|
1 | 1 | /* oxlint-disable no-sparse-arrays */ |
2 | 2 | import nodeAssert, { AssertionError } from 'node:assert' |
3 | 3 | import { stripVTControlCharacters } from 'node:util' |
4 | | -import { generateToBeMessage } from '@vitest/expect' |
| 4 | +import { equals, generateToBeMessage, iterableEquality } from '@vitest/expect' |
5 | 5 | import { processError } from '@vitest/utils/error' |
6 | 6 | import { assert, describe, expect, it, vi } from 'vitest' |
7 | 7 |
|
@@ -711,6 +711,47 @@ describe('.toStrictEqual()', () => { |
711 | 711 | }) |
712 | 712 | }) |
713 | 713 |
|
| 714 | +describe('typed arrays equality', () => { |
| 715 | + it('compares length and elements', () => { |
| 716 | + expect(Uint8Array.from([1, 2, 3])).toEqual(Uint8Array.from([1, 2, 3])) |
| 717 | + expect(Uint8Array.from([1, 2, 3])).not.toEqual(Uint8Array.from([1, 2, 4])) |
| 718 | + expect(Uint8Array.from([1, 2])).not.toEqual(Uint8Array.from([1, 2, 3])) |
| 719 | + }) |
| 720 | + |
| 721 | + it('compares NaN and signed zero like numbers', () => { |
| 722 | + expect(Float64Array.from([Number.NaN])).toEqual(Float64Array.from([Number.NaN])) |
| 723 | + expect(Float64Array.from([0])).not.toEqual(Float64Array.from([-0])) |
| 724 | + }) |
| 725 | + |
| 726 | + it('compares own properties that are not indexes', () => { |
| 727 | + const create = (props: Record<string, unknown> = {}) => |
| 728 | + Object.assign(Uint8Array.from([1, 2]), props) |
| 729 | + |
| 730 | + expect(create({ label: 'a' })).toEqual(create({ label: 'a' })) |
| 731 | + expect(create({ label: 'a' })).not.toEqual(create({ label: 'b' })) |
| 732 | + expect(create({ label: 'a' })).not.toEqual(create({ name: 'a' })) |
| 733 | + expect(create({ label: 'a' })).not.toEqual(create()) |
| 734 | + }) |
| 735 | + |
| 736 | + it('calls testers only for elements that differ', () => { |
| 737 | + const compared: unknown[] = [] |
| 738 | + const recordNumbers = (a: unknown, b: unknown) => { |
| 739 | + if (typeof a === 'number') { |
| 740 | + compared.push([a, b]) |
| 741 | + } |
| 742 | + return undefined |
| 743 | + } |
| 744 | + |
| 745 | + expect( |
| 746 | + equals(Uint8Array.from([1, 2, 3]), Uint8Array.from([1, 2, 4]), [ |
| 747 | + recordNumbers, |
| 748 | + iterableEquality, |
| 749 | + ]), |
| 750 | + ).toBe(false) |
| 751 | + expect(compared).toEqual([[3, 4]]) |
| 752 | + }) |
| 753 | +}) |
| 754 | + |
714 | 755 | describe('toBeTypeOf()', () => { |
715 | 756 | it.each([ |
716 | 757 | [1n, 'bigint'], |
|
0 commit comments