vitest-dev / vitest-dev/vitest

Vitest considers `-0` and `+0` to be different values

Open
#7,667 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

p2-nice-to-have
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Describe the bug

I have a utility that may return -0.

This is technically a distinct value from +0, but per IEEE_754, "negative zero" and "positive zero" should be considered to be equal in numerical comparisons.

But vitest's .toBe() and .toEqual() methods appears to not consider them to be equal, causing unit tests to fail.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-dgy7dtwc?file=test%2Fbasic.test.ts

System Info
The issue is reproduced in StackBlitz above, but here is my system info.

  System:
    OS: Linux 6.8 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (20) x64 13th Gen Intel(R) Core(TM) i5-13600K
    Memory: 19.04 GB / 31.12 GB
    Container: Yes
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 20.13.1 - ~/.nvm/versions/node/v20.13.1/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.13.1/bin/yarn
    npm: 10.5.2 - ~/.nvm/versions/node/v20.13.1/bin/npm
    pnpm: 8.15.9 - ~/.nvm/versions/node/v20.13.1/bin/pnpm
  npmPackages:
    @vitejs/plugin-react-swc: ^3.8.0 => 3.8.0 
    @vitest/coverage-v8: ^3.0.6 => 3.0.6 
    @vitest/ui: ^3.0.6 => 3.0.6 
    vite: ^6.1.0 => 6.1.0 
    vitest: ^3.0.6 => 3.0.6
Used Package Manager

pnpm

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked StackBlitz reproduction and trace the matcher implementations for .toBe() and .toEqual(). Add or update coverage for -0 and +0, with both matchers treating them as equal when the tests are complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.