Skip to content

Commit

Permalink
Improve failure messages for expect_visible() and `expect_invisible…
Browse files Browse the repository at this point in the history
…()` (#2001)

Fixes #1966
  • Loading branch information
hadley authored Oct 30, 2024
1 parent dd39c65 commit 8c2bd6e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
4 changes: 2 additions & 2 deletions R/expect-invisible.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/expect-invisible.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# generates useful failure messages

invisible(1) returns invisibly, not visibly.

---

1 returns visibly, not invisibly.

7 changes: 7 additions & 0 deletions tests/testthat/test-expect-invisible.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8c2bd6e

Please sign in to comment.