r-lib / testthat

An R 📦 to make testing 😀
https://testthat.r-lib.org
Other
884 stars 317 forks source link

vdiffr vs testthat::expect_snapshot() for plots #1973

Open fh-mthomson opened 1 month ago

fh-mthomson commented 1 month ago

Is vdiffr still recommended over testthat::expect_snapshot() for testing plots (e.g., ggplot2 objects)?

I assume yes (per https://github.com/r-lib/testthat/issues/617#issuecomment-333415894, https://github.com/r-lib/testthat/issues/1265#issuecomment-1721286411) but wanted to confirm, since testthat has seen a lot of great enhancements in the snapshot realm :)

Motivating example (modified from #1393): I was hoping (perhaps naively) to use:

test_that("ggplot", {
  library(ggplot2)
  expect_snapshot(ggplot(data.frame(), aes(1,2)) + geom_point())
})

But, this errors:

── Error ('test-ggplot.R:4:3'): ggplot ─────────────────────────────────────────
Error in `UseMethod("snapshot_replay", x)`: no applicable method for 'snapshot_replay' applied to an object of class "recordedplot"
Backtrace:
    ▆
 1. └─testthat::expect_snapshot(ggplot(data.frame(), aes(1, 2)) + geom_point()) at test-ggplot.R:4:3
 2.   ├─testthat:::with_is_snapshotting(...)
 3.   └─testthat:::verify_exec(quo_get_expr(x), quo_get_env(x), replay)
 4.     ├─base::unlist(lapply(results, replay))
 5.     └─base::lapply(results, replay)
 6.       └─testthat (local) FUN(X[[i]], ...)
 7.         └─testthat:::snapshot_replay(x, state, transform = transform, cnd_class = cnd_class)
fh-mthomson commented 1 month ago

Upon further evaluation, vdiffr has a lot of amazing features to enable comparisons across snapshots (side by side, slider, etc), so will use that indefinitely!