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

deep equality check failing message is very different compare to Jest #1290

Closed
4 tasks done
ghiscoding opened this issue May 11, 2022 · 3 comments
Closed
4 tasks done
Labels
enhancement New feature or request pr welcome

Comments

@ghiscoding
Copy link
Contributor

ghiscoding commented May 11, 2022

Clear and concise description of the problem

I recently switched from Jest to Vitest and very often in Jest I would use the .toBe() instead of .toEqual() since I never remember which one is which and in Jest when we do use the wrong one then we have a great useful message telling us that we probably meant to use another validity check. This is very helpful but in Vitest it just fails equality check without giving us any hint at all.

For example, if we have this test that we run in both libs

expect([{ id: 123, name: 'John', age: 30 }])
 .toBe([{ id: 123, name: 'John', age: 30 }]);

in Jest we have this helpful message "serializes to the same string" and also other suggestions

expect(received).toBe(expected) // Object.is equality

    If it should pass with deep equality, replace "toBe" with "toStrictEqual"

    Expected: [{"age": 30, "id": 123, "name": "John"}]
    Received: serializes to the same string

      at Object.<anonymous> (/src/index.spec.ts:34:50)

but in Vitest, it just fails without providing any hint of our problem

 FAIL  src/index.spec.ts > My Test Suite > should have the same array
AssertionError: expected [ { id: 123, name: 'John', age: 30 } ] to be [ { id: 123, name: 'John', age: 30 } ] // Object.is equality
 ❯ src/stores/__tests__/viewStore.spec.ts:63:49
     61|     expect(store.selectedTab).toBe('compId');
     62| 
     63|     expect([{ id: 123, name: 'John', age: 30 }]).toBe([{ id: 123, name: 'John', age: 30 }]);
       |                                                 ^
     64|   });
     65| });

A new developer using Vitest will most certainly be confused and won't know why it failed, then do a search on Google or Stack Overflow. However in Jest, it gives us a great hint of how to modify our code and use the correct check. Half of the time I use the wrong one, so I will very often see this failing... to be or not to be 😄

Suggested solution

Use the same message as Jest which helps user finding the issue with the test. The message that always remind me that I used the wrong check is

Received: serializes to the same string

then I know right away that I must use .toEqual() instead of .toBe()

A very simple message that can so so helpful

Alternative

No response

Additional context

I ran the same test with both libs at latest versions, Jest 28 and Vitest 0.12.4

Validations

@ghiscoding ghiscoding changed the title deep equality check failing message is not helpful enough deep equality check failing message is very different compare to Jest May 12, 2022
chaii3 pushed a commit to chaii3/vitest that referenced this issue May 13, 2022
@sheremet-va sheremet-va added enhancement New feature or request pr welcome labels May 20, 2022
@TommyDew42
Copy link
Contributor

Hi! Can I help with this?

@ghiscoding
Copy link
Contributor Author

@TommyDew42
I would say yes go ahead, the issue was tagged as "pr welcome" and I don't think anyone started working on it.

@ghiscoding
Copy link
Contributor Author

closed by #1383

@github-actions github-actions bot locked and limited conversation to collaborators Jun 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request pr welcome
Projects
None yet
Development

No branches or pull requests

3 participants