Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): DataView comparison does not work in toStrictEqual #3703

Merged
merged 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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