Skip to content

Commit c32520b

Browse files
authored
Entrace unexpected errors in snapshots (#2283)
This ensures that their backtraces point to the source of the error, not the internals of `expect_snapshot()` Fixes #2277
1 parent b26a71f commit c32520b

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* `expect_snapshot()` on reports how to resolve failures once when running inside `R CMD check`.
4747
* `expect_snapshot()` no longer skips on CRAN, as that skips the rest of the test. Instead it just returns, neither succeeding nor failing (#1585).
4848
* `expect_snapshot()` and `expect_snapshot_file()` hints now include the path to the package, if it's not the current working directory (#1577).
49+
* `expect_snapshot()` gives a more informative backtrace when the code inside the snapshot errors (#2277).
4950
* `expect_snapshot_file()` now clearly errors if the `path` doesn't exist (#2191), or has been used alreaday (#1592). It now considers `.json` to be a text file (#1593), and shows differences for text files in the console (#1593).
5051
* `expect_snapshot_value()` can now handle expressions that generate `-` (#1678) or zero length atomic vectors (#2042).
5152
* `expect_vector()` fails, instead of erroring, if `object` is not a vector (@plietar, #2224).

R/verify-output.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ verify_exec <- function(expr, env = caller_env(), replay = output_replay) {
8484
})
8585
source <- unlist(exprs, recursive = FALSE)
8686

87-
handler <- evaluate::new_output_handler(value = testthat_print)
87+
handler <- evaluate::new_output_handler(
88+
value = testthat_print,
89+
calling_handlers = list(error = function(cnd) rlang::entrace(cnd))
90+
)
8891
results <- evaluate::evaluate(
8992
source,
9093
envir = env,

tests/testthat/_snaps/reporter-progress.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@
192192
- | 7 1 1 | reporters/backtraces
193193
\ | 8 1 1 | reporters/backtraces
194194
| | 9 1 1 | reporters/backtraces
195-
x | 9 1 1 | reporters/backtraces
195+
/ | 10 1 1 | reporters/backtraces
196+
x | 10 1 1 | reporters/backtraces
196197
--------------------------------------------------------------------------------
197198
Error ('reporters/backtraces.R:3:8'): errors thrown at block level are entraced
198199
Error in `g()`: foo
@@ -294,7 +295,15 @@
294295
25. \-f(x - 1)
295296
26. \-f(x - 1)
296297

297-
Failure ('reporters/backtraces.R:62:6'): (code run outside of `test_that()`)
298+
Error ('reporters/backtraces.R:64:3'): errors in snapshots get useful backtraces
299+
Error in `h()`: !
300+
Backtrace:
301+
x
302+
1. \-f()
303+
2. \-g()
304+
3. \-h()
305+
306+
Failure ('reporters/backtraces.R:70:6'): (code run outside of `test_that()`)
298307
Expected FALSE to be TRUE.
299308
Differences:
300309
`actual`: FALSE
@@ -307,7 +316,7 @@
307316
3. \-h()
308317
4. \-testthat::expect_true(FALSE)
309318

310-
Failure ('reporters/backtraces.R:67:3'): nested expectations get backtraces
319+
Failure ('reporters/backtraces.R:75:3'): nested expectations get backtraces
311320
Expected FALSE to be TRUE.
312321
Differences:
313322
`actual`: FALSE
@@ -416,7 +425,15 @@
416425
25. \-f(x - 1)
417426
26. \-f(x - 1)
418427

419-
Failure ('reporters/backtraces.R:62:6'): (code run outside of `test_that()`)
428+
Error ('reporters/backtraces.R:64:3'): errors in snapshots get useful backtraces
429+
Error in `h()`: !
430+
Backtrace:
431+
x
432+
1. \-f()
433+
2. \-g()
434+
3. \-h()
435+
436+
Failure ('reporters/backtraces.R:70:6'): (code run outside of `test_that()`)
420437
Expected FALSE to be TRUE.
421438
Differences:
422439
`actual`: FALSE
@@ -429,7 +446,7 @@
429446
3. \-h()
430447
4. \-testthat::expect_true(FALSE)
431448

432-
Failure ('reporters/backtraces.R:67:3'): nested expectations get backtraces
449+
Failure ('reporters/backtraces.R:75:3'): nested expectations get backtraces
433450
Expected FALSE to be TRUE.
434451
Differences:
435452
`actual`: FALSE
@@ -442,7 +459,7 @@
442459
3. \-h()
443460
4. \-testthat::expect_true(FALSE)
444461

445-
[ FAIL 9 | WARN 1 | SKIP 0 | PASS 1 ]
462+
[ FAIL 10 | WARN 1 | SKIP 0 | PASS 1 ]
446463

447464
No one gets it right on their first try
448465

tests/testthat/_snaps/snapshot-reporter.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
Code
44
err$message
55
Output
6-
[1] "Expected NULL to throw a error."
6+
[1] "Error: Expected NULL to throw a error."
77

tests/testthat/_snaps/snapshot.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@
243243
Condition <simpleWarning>
244244
Warning in `f()`:
245245
bar
246-
Condition <simpleError>
246+
Condition <rlang_error>
247247
Error in `f()`:
248248
! baz
249249

tests/testthat/reporters/backtraces.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ test_that("deep stacks are shown", {
5656
f(25)
5757
})
5858

59+
test_that("errors in snapshots get useful backtraces", {
60+
f <- function() g()
61+
g <- function() h()
62+
h <- function() stop("!")
63+
64+
expect_snapshot(f())
65+
})
66+
5967
# Expectations ----------------------------------------------------------------
6068
f <- function() g()
6169
g <- function() h()

tests/testthat/test-reporter-progress.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ test_that("can fully suppress incremental updates", {
4343
})
4444

4545
test_that("reports backtraces", {
46+
withr::local_envvar(TESTTHAT_MAX_FAILS = Inf)
4647
expect_snapshot_reporter(
4748
ProgressReporter$new(update_interval = 0, min_time = Inf),
4849
test_path("reporters/backtraces.R")

0 commit comments

Comments
 (0)