From 04519fe9d9fb912c03d38f9e04be7fc679c0485b Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 29 Nov 2023 12:20:40 -0600 Subject: [PATCH] Re-signal unexpected snapshot errors (#1910) Fixes #1906 --- NEWS.md | 4 ++++ R/snapshot.R | 2 +- tests/testthat/test-snapshot.R | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 65432a2dd..9abdfcb39 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # testthat (development version) +* Unexpected errors in `expect_snapshot()` now behave like regular errors; + i.e. they terminate evaluation of the entire test and get a traceback + (#1906). + * `local_mocked_bindings()` can now mock any object, not just functions (#1896). diff --git a/R/snapshot.R b/R/snapshot.R index b5ef5fe4c..421933f59 100644 --- a/R/snapshot.R +++ b/R/snapshot.R @@ -94,7 +94,7 @@ expect_snapshot <- function(x, if (error) { expect(FALSE, msg, trace = state$error[["trace"]]) } else { - exp_signal(expectation("error", msg, trace = state$error[["trace"]])) + cnd_signal(state$error) } return() } diff --git a/tests/testthat/test-snapshot.R b/tests/testthat/test-snapshot.R index 3e0372550..ab35726b4 100644 --- a/tests/testthat/test-snapshot.R +++ b/tests/testthat/test-snapshot.R @@ -142,6 +142,18 @@ test_that("errors and warnings are folded", { expect_snapshot(error = TRUE, f()) }) +# I don't know how to test this automatically; wrapping it in another +# snapshot doesn't capture the behaviour I expected, presumably due to the +# way that errors bubble up +# test_that("errors in snapshots behave like regular errors", { +# f <- function() g() +# g <- function() h() +# h <- function() abort("!") +# +# expect_snapshot(f()) +# expect_snapshot(1 + 1) +# }) + test_that("hint is informative", { local_reproducible_output(crayon = TRUE, hyperlinks = TRUE, rstudio = TRUE)