Mistake: Testing Floating Point
Find the Mistake Medium
Question
What is wrong with this floating-point test?
Code
test('decimal', () => {
expect(0.1 + 0.2).toBe(0.3);
});
Hint
Floating-point arithmetic is imprecise in JavaScript.
Explanation
The mistake is using exact equality for floating-point numbers. Due to IEEE 754 representation, 0.1 + 0.2 equals 0.30000000000000004, not 0.3. The fix: use toBeCloseTo: expect(0.1 + 0.2).toBeCloseTo(0.3);, which checks equality within a small tolerance.
Related Resources
Related Lessons
- Jest Basics
Getting started with Jest.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.