AssertJ unexpectedly returns true when checking if a double is close to NaN.
```java
assertThat(Double.NaN).isCloseTo(0.00811, Percentage.withPercentage(0.1));
Is this a bug? The version used is 3.6.2.
AssertJ unexpectedly returns true when checking if a double is close to NaN.
```java
assertThat(Double.NaN).isCloseTo(0.00811, Percentage.withPercentage(0.1));
Is this a bug? The version used is 3.6.2.
I ran into this as well and realized it’s not exactly a bug but a quirk in older versions of AssertJ (like 3.6.2). In that version, isCloseTo doesn’t handle NaN properly—it treats it as “not comparable” and can return true unexpectedly. Upgrading to a newer AssertJ (3.24+) fixed this in my projects because newer versions explicitly check for NaN and Infinity.
I also noticed that waitFor sometimes fails in headless CI runs because the component renders asynchronously. Switching to findBy queries made the tests more reliable:
for (const status of statuses) {
const element = await screen.findByText(status, {}, { timeout: 3000 });
expect(element).toBeInTheDocument();
}
findBy internally waits for the element to appear, which avoids flaky waitFor usage. It’s cleaner and often fixes CI-only failures.
From my experience, any comparison using Percentage or Offset with NaN can give unexpected results, because NaN is unordered (NaN != NaN). I usually guard my tests like this:
double value = calculateSomething();
if (Double.isNaN(value)) {
assertThat(value).isNaN();
} else {
assertThat(value).isCloseTo(0.00811, Percentage.withPercentage(0.1));
}
This pattern prevents false positives when NaN sneaks in and keeps numeric comparisons safe.