cryptic error when `expect_equal(x, y, tolerance = -Inf)`

Open
#2,344 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start by reproducing the provided MRE through testthat::expect_equal(), then trace the tolerance handling through testthat:::waldo_compare() and waldo::compare(), as shown in the backtrace. Identify where tolerance = -Inf becomes NaN and add regression coverage that verifies the resulting diagnostic is informative rather than cryptic.

Written by the indexing model from the issue text.

Description

Hi,

This is not really a critical bug...

I was writing tests and hoped expect_equal(actual, expected, tolerance = -Inf) would use the "lowest possible tolerance". I did not even expect it to work. I am reporting because of the cryptic error message.

MRE:

# Success
test_that("1 = 1 with minimum tolerance", {
  expect_equal(1,1,tolerance = -Inf)
})

# Cryptic Error message
test_that("1 = 1.1 with minimum tolerance", {
  expect_equal(1,1.1,tolerance = -Inf) # does not work
})

# Success
test_that("1 = 1.1 with maximum tolerance", {
  expect_equal(1,1.1,tolerance = Inf)
})

error message

── Warning: 1 = 1.1 with minimum tolerance ───────────────────────────────────
NaNs produced
Backtrace:
    ▆
 1. └─testthat::expect_equal(1, 1.1, tolerance = -Inf)
 2.   └─testthat:::expect_waldo_equal_(msg, act, exp, info, ..., tolerance = tolerance)
 3.     └─testthat:::waldo_compare(...)
 4.       └─waldo::compare(x, y, ..., x_arg = x_arg, y_arg = y_arg)
 5.         └─waldo:::compare_structure(x, y, paths = c(x_arg, y_arg), opts = opts)
 6.           └─waldo:::compare_vector(x, y, paths = paths, opts = opts)
 7.             └─waldo:::compare_numeric(...)
 8.               └─waldo:::min_digits(x, y, tolerance)
 9.                 └─waldo:::digits(tolerance)

> packageVersion("testthat")
[1] ‘3.3.2’

> packageVersion("waldo")
[1] ‘0.6.2’

> version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          6.1                         
year           2026                        
month          06                          
day            24                          
svn rev        90187                       
language       R                           
version.string R version 4.6.1 (2026-06-24)
nickname       Happy Hop                   

Dominant language
R
Stars
939
Forks
346
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

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.

More from r-lib/testthat

All issues in r-lib/testthat

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.