Skip to content

Commit

Permalink
Improve round trip error message
Browse files Browse the repository at this point in the history
Fixes #1771
  • Loading branch information
hadley committed Sep 22, 2023
1 parent fe01a63 commit 5e793ed
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# testthat (development version)

* `expect_snapshot_value()` has an improved error if the object can't be
safely seriazlied using the specified `style` (#1771).

* `skip_if_offline()` now errors if you don't have curl installed (#1854).

* All packages, regardless of whether or not they use rlang 1.0.0, now
Expand Down
19 changes: 14 additions & 5 deletions R/snapshot-reporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ SnapshotReporter <- R6::R6Class("SnapshotReporter",
comp
} else {
value_enc <- save(value)
check_roundtrip(value, load(value_enc), ..., tolerance = tolerance)

self$cur_snaps$append(self$test, variant, value_enc)

Expand Down Expand Up @@ -161,12 +160,22 @@ SnapshotReporter <- R6::R6Class("SnapshotReporter",
)


check_roundtrip <- function(x, y, ..., tolerance = testthat_tolerance()) {
check <- waldo_compare(x, y, x_arg = "value", y_arg = "roundtrip", ..., tolerance = tolerance)
check_roundtrip <- function(x,
y,
style,
...,
tolerance = testthat_tolerance(),
error_call = caller_env()) {
check <- waldo_compare(x, y, x_arg = "original", y_arg = "new", ..., tolerance = tolerance)
if (length(check) > 0) {
abort(c(
paste0("Serialization round-trip is not symmetric.\n\n", check, "\n"),
i = "You may need to consider serialization `style`")
paste0("Object could not be safely serialized with `style = \"", style, "\"`."),
" " = paste0(
"Serializing then deserializing the object returned something new:\n\n",
check, "\n"
),
i = "You may need to try a different `style`."),
call = error_call
)
}
}
Expand Down
2 changes: 2 additions & 0 deletions R/snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ expect_snapshot_value <- function(x,
)

with_is_snapshotting(force(x))
check_roundtrip(x, load(save(x)), style = style, tolerance = tolerance)

expect_snapshot_helper(lab, x,
save = save,
load = load,
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/_snaps/snapshot-reporter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# check_roundtrip() gives nice error

Code
wrapper(NULL, list(), style = "json")
Condition
Error in `wrapper()`:
! Object could not be safely serialized with `style = "json"`.
Serializing then deserializing the object returned something new:
`original` is NULL
`new` is a list
i You may need to try a different `style`.

10 changes: 9 additions & 1 deletion tests/testthat/test-snapshot-reporter.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,15 @@ test_that("errors if can't roundtrip", {
snapper <- local_snapshotter()
snapper$start_file("snapshot-4", "test")

expect_error(expect_snapshot_value(NULL), "not symmetric")
expect_error(expect_snapshot_value(NULL), "safely serialized")
})

test_that("check_roundtrip() gives nice error", {
# disable crayon usage
local_bindings(crayon = FALSE, .env = get_reporter())

wrapper <- function(...) check_roundtrip(...)
expect_snapshot(wrapper(NULL, list(), style = "json"), error = TRUE)
})

test_that("errors in test doesn't change snapshot", {
Expand Down

0 comments on commit 5e793ed

Please sign in to comment.