Skip to content

Commit 957cfad

Browse files
authored
Ensure that expect_snapshot() registers restart (#2271)
Renamed `continue_test` to `muffle_expectation` to make it more clear what the purporse of this restart is — it's to allow us to opt-out of further handling.
1 parent 02752da commit 957cfad

File tree

8 files changed

+25
-11
lines changed

8 files changed

+25
-11
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ This release mostly focusses on an overhaul of how testthat works with condition
850850

851851
* New `exp_signal()` function is a condition signaller that
852852
implements the testthat protocol (signal with `stop()` if the
853-
expectation is broken, with a `continue_test` restart).
853+
expectation is broken, with a `muffle_expectation` restart).
854854

855855
* Existence of restarts is first checked before invocation. This makes
856856
it possible to signal warnings or messages with a different

R/expect-self-test.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ capture_success_failure <- function(expr) {
1111
expectation_failure = function(cnd) {
1212
last_failure <<- cnd
1313
n_failure <<- n_failure + 1
14-
# Don't bubble up to any other handlers
15-
invokeRestart("continue_test")
14+
invokeRestart("muffle_expectation")
1615
},
1716
expectation_success = function(cnd) {
1817
n_success <<- n_success + 1
19-
# Don't bubble up to any other handlers
20-
invokeRestart("continue_test")
18+
invokeRestart("muffle_expectation")
2119
}
2220
)
2321

R/expectation.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' optionally subsequence) elements should describe what was actually seen.
1111
#' @inheritParams fail
1212
#' @return An expectation object from either `succeed()` or `fail()`.
13-
#' with a `continue_test` restart.
13+
#' with a `muffle_expectation` restart.
1414
#' @seealso [exp_signal()]
1515
#' @keywords internal
1616
#' @export
@@ -104,7 +104,7 @@ exp_signal <- function(exp) {
104104
} else {
105105
signalCondition(exp)
106106
},
107-
continue_test = function(e) NULL
107+
muffle_expectation = function(e) NULL
108108
)
109109

110110
invisible(exp)

R/snapshot.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,12 @@ expect_snapshot_ <- function(
131131
if (error) {
132132
fail(msg, trace = state$error[["trace"]])
133133
} else {
134-
cnd_signal(state$error)
134+
# This might be a failed expectation, so we need to make sure
135+
# that we can muffle it
136+
withRestarts(
137+
cnd_signal(state$error),
138+
muffle_expectation = function() NULL
139+
)
135140
}
136141
return()
137142
}

R/test-that.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ test_code <- function(code, env, reporter = NULL, skip_on_empty = TRUE) {
111111
handle_expectation <- function(e) {
112112
the$test_expectations <- the$test_expectations + 1L
113113
register_expectation(e, 7)
114-
# Don't bubble up to any other handlers
115-
invokeRestart("continue_test")
114+
invokeRestart("muffle_expectation")
116115
}
117116
handle_warning <- function(e) {
118117
# When options(warn) < 0, warnings are expected to be ignored.

man/expect.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/snapshot.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@
104104
Error:
105105
! Expected `print("!")` to throw a error.
106106

107+
# snapshots of failures fail
108+
109+
Code
110+
expect_snapshot(fail())
111+
Condition
112+
Error:
113+
! Failure has been forced
114+
107115
# can capture error/warning messages
108116

109117
This is an error

tests/testthat/test-snapshot.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ test_that("always checks error status", {
6969
expect_snapshot_failure(expect_snapshot(print("!"), error = TRUE))
7070
})
7171

72+
test_that("snapshots of failures fail", {
73+
expect_snapshot_failure(expect_snapshot(fail()))
74+
})
75+
7276
test_that("can capture error/warning messages", {
7377
expect_snapshot_error(stop("This is an error"))
7478
expect_snapshot_warning(warning("This is a warning"))

0 commit comments

Comments
 (0)