Skip to content

Commit 52aef92

Browse files
Dunqingsheremet-va
authored andcommitted
fix(core): DataView comparison does not work in toStrictEqual
1 parent a78e6bd commit 52aef92

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

packages/expect/src/jest-utils.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,16 @@ export function typeEquality(a: any, b: any): boolean | undefined {
478478

479479
export function arrayBufferEquality(a: unknown,
480480
b: unknown): boolean | undefined {
481-
if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer))
482-
return undefined
481+
let dataViewA = a as DataView
482+
let dataViewB = b as DataView
483+
484+
if (!(a instanceof DataView && b instanceof DataView)) {
485+
if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer))
486+
return undefined
483487

484-
const dataViewA = new DataView(a)
485-
const dataViewB = new DataView(b)
488+
dataViewA = new DataView(a)
489+
dataViewB = new DataView(b)
490+
}
486491

487492
// Buffers are not equal when they do not have the same byte length
488493
if (dataViewA.byteLength !== dataViewB.byteLength)

test/core/test/jest-expect.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,25 @@ describe('.toStrictEqual()', () => {
466466
Uint8Array.from([9, 3]).buffer,
467467
)
468468
})
469+
470+
it('does not pass for DataView', () => {
471+
expect(new DataView(Uint8Array.from([1, 2, 3]).buffer)).not.toStrictEqual(
472+
new DataView(Uint8Array.from([3, 2, 1]).buffer),
473+
)
474+
475+
expect(new DataView(Uint16Array.from([1, 2]).buffer)).not.toStrictEqual(
476+
new DataView(Uint16Array.from([2, 1]).buffer),
477+
)
478+
})
479+
480+
it('passes for matching DataView', () => {
481+
expect(new DataView(Uint8Array.from([1, 2, 3]).buffer)).toStrictEqual(
482+
new DataView(Uint8Array.from([1, 2, 3]).buffer),
483+
)
484+
expect(new DataView(Uint8Array.from([]).buffer)).toStrictEqual(
485+
new DataView(Uint8Array.from([]).buffer),
486+
)
487+
})
469488
})
470489

471490
describe('toBeTypeOf()', () => {

0 commit comments

Comments
 (0)