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

Handling of ArrayBuffer in .toStrictEqual() #11770

Closed
SPodjasek opened this issue Aug 20, 2021 · 1 comment · Fixed by #11805
Closed

Handling of ArrayBuffer in .toStrictEqual() #11770

SPodjasek opened this issue Aug 20, 2021 · 1 comment · Fixed by #11805

Comments

@SPodjasek
Copy link

SPodjasek commented Aug 20, 2021

🚀 Feature Proposal (or maybe 🐛 Bug Report)

Note: This may be in some way related to previously reported bug #6109. But that case didn't come with enough details for some acceptable solution.

I'm writing some tests for data structures based on ArrayBuffers and have run into an issue with the code below:

expect(Uint8Array.from([1, 2]).buffer).toStrictEqual(Uint8Array.from([0, 0]).buffer);

I expected this to fail, but it passed.
Looking through .toStrictEqual() I see that it compares the .byteLength field only - as contents of ArrayBuffer is not directly accessible.
To compare ArrayBuffers, You need to wrap it as DataView and compared byte-by-byte.

I know that this may be out of .toStrictEqual() scope, but IMHO even if its not implemented there it deserves some kind of warning when this matcher is passed ArrayBuffer as arguments.

Motivation

If this should not be implemented in core, at least warn users about possible false positives.

Example

$ jest
 FAIL  lib/arraybuffer.spec.ts
  ✕ invalid input (8 ms)

  ● compare array buffers

    expect(received).toStrictEqual(expected) // deep equality

    ! Note: comparing `ArrayBuffer` can give you false-positives

Pitch

  • Additional warning for base matcher, or add support for a 'special care' data type...
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant