From 8c2bd6efa9d338b9f340b8a108adb22e0b3c432b Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 30 Oct 2024 10:47:12 -0500 Subject: [PATCH] Improve failure messages for `expect_visible()` and `expect_invisible()` (#2001) Fixes #1966 --- NEWS.md | 1 + R/expect-invisible.R | 4 ++-- tests/testthat/_snaps/expect-invisible.md | 8 ++++++++ tests/testthat/test-expect-invisible.R | 7 +++++++ 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/_snaps/expect-invisible.md diff --git a/NEWS.md b/NEWS.md index e833c445b..204416428 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # testthat (development version) +* `expect_visible()` and `expect_invisible()` have improved failure messages (#1966). * `expect_snapshot()` now strips line breaks in test descriptions (@LDSamson, #1900). * `expect_snapshot()` now errors when called from a `test_that()` that has an empty description (@kevinushey, #1980). * `skip_if_not_installed()` produces a clearer message (@MichaelChirico, #1959). diff --git a/R/expect-invisible.R b/R/expect-invisible.R index 16306b5d9..4b2f576ff 100644 --- a/R/expect-invisible.R +++ b/R/expect-invisible.R @@ -26,7 +26,7 @@ expect_invisible <- function(call, label = NULL) { expect( identical(vis$visible, FALSE), - sprintf("%s does not return invisibly", lab) + sprintf("%s returns visibly, not invisibly.", lab) ) invisible(vis$value) } @@ -39,7 +39,7 @@ expect_visible <- function(call, label = NULL) { expect( identical(vis$visible, TRUE), - sprintf("%s does not invisibly", lab) + sprintf("%s returns invisibly, not visibly.", lab) ) invisible(vis$value) } diff --git a/tests/testthat/_snaps/expect-invisible.md b/tests/testthat/_snaps/expect-invisible.md new file mode 100644 index 000000000..bdd7b69fc --- /dev/null +++ b/tests/testthat/_snaps/expect-invisible.md @@ -0,0 +1,8 @@ +# generates useful failure messages + + invisible(1) returns invisibly, not visibly. + +--- + + 1 returns visibly, not invisibly. + diff --git a/tests/testthat/test-expect-invisible.R b/tests/testthat/test-expect-invisible.R index c9264a53b..5c105824d 100644 --- a/tests/testthat/test-expect-invisible.R +++ b/tests/testthat/test-expect-invisible.R @@ -6,6 +6,13 @@ test_that("basic principles of visibility hold", { expect_failure(expect_visible(x <- 1)) }) +test_that("generates useful failure messages", { + + expect_snapshot_failure(expect_visible(invisible(1))) + expect_snapshot_failure(expect_invisible(1)) + +}) + test_that("invisibly returns evaluated value", { out <- expect_invisible(expect_invisible(x <- 2 + 2)) expect_equal(out, 4)