Skip to content

fix(core): toBeOdd fails on negative odd numbers - #209

Open
KeylaMunnoz wants to merge 4 commits into
mainfrom
fix/issue-191-to-be-odd-negative
Open

KeylaMunnoz wants to merge 4 commits into
mainfrom
fix/issue-191-to-be-odd-negative

Conversation

@KeylaMunnoz

Copy link
Copy Markdown
Collaborator

Fixes #191

Description

In JavaScript, the remainder operator (%) retains the sign of the dividend (e.g., -13 % 2 === -1). As a result, NumberAssertion.toBeOdd() was failing for negative odd numbers because it checked this.actual % 2 === 1.

Changes

  • Fixed NumberAssertion.toBeOdd to check Math.abs(this.actual % 2) === 1.
  • Documented parity behavior in JSDoc for both toBeEven and toBeOdd, clarifying that parity applies strictly to integers: non-integers, NaN, and Infinity are neither even nor odd, while 0 and -0 are considered even.
  • Added comprehensive unit tests in NumberAssertion.test.ts covering negative odd numbers (-13), negative even numbers (-2), 0, -0, non-integers (1.5, -1.5), NaN, and Infinity.

@KeylaMunnoz
KeylaMunnoz requested a review from rojasTob November 13, 2026 13:35
@rojasTob

rojasTob commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for the fix! Math.abs(this.actual % 2) === 1 handles negative odd numbers, fractions, NaN, ±Infinity and -0 correctly. A few things before merging:

Please add these tests:

  • -1 for toBeOdd (the smallest negative odd number)
  • -0 for toBeOdd (it should fail directly and pass under .not)
  • -Infinity for both matchers, and -1.5 for toBeEven

The bug only shows up with negative numbers, so these are the cases most likely to catch a regression. AGENTS.md also asks for a boundary case and a negative-number case for this kind of matcher.

Optional:

  • The .not errors for toBeOdd/toBeEven don't show the value ("Expected the value NOT to be odd"). Since you're already in these methods, Expected <${this.actual}> NOT to be odd would match the other matchers.
  • Number.isInteger(x) && x % 2 !== 0 states the "only integers have parity" rule from your JSDoc more directly than relying on % returning NaN or a fraction.
  • The JSDoc note mentions Infinity; it could say ±Infinity.

Happy to approve once the tests are in.

@KeylaMunnoz

Copy link
Copy Markdown
Collaborator Author

Hey @rojasTob ! Thanks for the review !
Added all the requested test cases for negative numbers, negative zero, negative infinity, and negative decimals, including the expected behavior with .not.

@rojasTob rojasTob left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Key! LGTM 👍

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

toBeOdd fails on negative odd numbers (false pass with .not)

2 participants