Skip to content

Commit

Permalink
fix(core): DataView comparison does not work in toStrictEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing authored and sheremet-va committed Jul 11, 2023
1 parent a78e6bd commit 52aef92
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/expect/src/jest-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,16 @@ export function typeEquality(a: any, b: any): boolean | undefined {

export function arrayBufferEquality(a: unknown,
b: unknown): boolean | undefined {
if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer))
return undefined
let dataViewA = a as DataView
let dataViewB = b as DataView

if (!(a instanceof DataView && b instanceof DataView)) {
if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer))
return undefined

const dataViewA = new DataView(a)
const dataViewB = new DataView(b)
dataViewA = new DataView(a)
dataViewB = new DataView(b)
}

// Buffers are not equal when they do not have the same byte length
if (dataViewA.byteLength !== dataViewB.byteLength)
Expand Down
19 changes: 19 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,25 @@ describe('.toStrictEqual()', () => {
Uint8Array.from([9, 3]).buffer,
)
})

it('does not pass for DataView', () => {
expect(new DataView(Uint8Array.from([1, 2, 3]).buffer)).not.toStrictEqual(
new DataView(Uint8Array.from([3, 2, 1]).buffer),
)

expect(new DataView(Uint16Array.from([1, 2]).buffer)).not.toStrictEqual(
new DataView(Uint16Array.from([2, 1]).buffer),
)
})

it('passes for matching DataView', () => {
expect(new DataView(Uint8Array.from([1, 2, 3]).buffer)).toStrictEqual(
new DataView(Uint8Array.from([1, 2, 3]).buffer),
)
expect(new DataView(Uint8Array.from([]).buffer)).toStrictEqual(
new DataView(Uint8Array.from([]).buffer),
)
})
})

describe('toBeTypeOf()', () => {
Expand Down

0 comments on commit 52aef92

Please sign in to comment.