fix: use Object.is in replaceEqualDeep for correct NaN handling#10329
fix: use Object.is in replaceEqualDeep for correct NaN handling#10329alex-js-ltd wants to merge 3 commits intoTanStack:mainfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughReplaced Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/query-core/src/__tests__/utils.test.tsx (1)
419-424: Good test coverage for the NaN fix.The test correctly validates that structural sharing works when objects contain
NaNvalues.For completeness, consider adding tests for:
- Top-level primitives:
expect(replaceEqualDeep(NaN, NaN)).toBe(NaN)- Arrays containing
NaN:expect(replaceEqualDeep([NaN], [NaN])).toBe(prev)🧪 Additional test cases
it('should return the previous object reference when values contain NaN', () => { const prev = { value: NaN } const next = { value: NaN } expect(replaceEqualDeep(prev, next)).toBe(prev) }) + + it('should return the previous value when both values are NaN', () => { + expect(Number.isNaN(replaceEqualDeep(NaN, NaN))).toBe(true) + }) + + it('should return the previous array reference when values contain NaN', () => { + const prev = [NaN, { a: 1 }] + const next = [NaN, { a: 1 }] + expect(replaceEqualDeep(prev, next)).toBe(prev) + })🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/query-core/src/__tests__/utils.test.tsx` around lines 419 - 424, Add two more tests for replaceEqualDeep: one that verifies top-level primitives with NaN return the same primitive (use expect(replaceEqualDeep(NaN, NaN)).toBe(NaN)), and one that verifies arrays with NaN preserve structural sharing (create const prev = [NaN]; const next = [NaN]; expect(replaceEqualDeep(prev, next)).toBe(prev)). Ensure both tests live alongside the existing NaN test and reference the replaceEqualDeep function.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/query-core/src/__tests__/utils.test.tsx`:
- Around line 419-424: Add two more tests for replaceEqualDeep: one that
verifies top-level primitives with NaN return the same primitive (use
expect(replaceEqualDeep(NaN, NaN)).toBe(NaN)), and one that verifies arrays with
NaN preserve structural sharing (create const prev = [NaN]; const next = [NaN];
expect(replaceEqualDeep(prev, next)).toBe(prev)). Ensure both tests live
alongside the existing NaN test and reference the replaceEqualDeep function.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 35d095f1-e907-44e9-90e4-f2bd81f742ab
📒 Files selected for processing (3)
.changeset/happy-banks-repeat.mdpackages/query-core/src/__tests__/utils.test.tsxpackages/query-core/src/utils.ts
🎯 Changes
replaceEqualDeeppreviously used===for equality checks.This causes an issue with
NaN, sinceNaN === NaNisfalse. As a result, values containingNaNare treated as unequal, preventing structural sharing and causing unnecessary object replacement.This PR replaces
===withObject.is, which correctly treats:NaNas equal toNaN+0and-0as distinctA test has been added to verify that objects containing
NaNpreserve reference equality.✅ Checklist
pnpm run test:pr.🚀 Release Impact
Summary by CodeRabbit
Bug Fixes
Tests