# Snapshot tests, end to end: builds a throwaway package whose tests snapshot a # printed summary with expect_snapshot() and a plot with # vdiffr::expect_doppelganger(). Runs the tests twice (record, then compare), # then changes the output so a snapshot fails. # # Rscript snapshot-demo.R demo_files <- list( "R/age-summary.R" = c( "#' Print a small age summary", "#' @export", "print_age_summary <- function(age) {", " if (!is.numeric(age)) stop(\"`age` must be numeric.\", call. = FALSE)", " cat(\"n: \", length(age), \"\\n\", sep = \"\")", " cat(\"Mean (SD):\", sprintf(\"%.1f (%.1f)\", mean(age), stats::sd(age)), \"\\n\")", " invisible(age)", "}" ), "R/age-plot.R" = c( "#' Box plot of age by arm", "#' @export", "age_plot <- function(data) {", " ggplot2::ggplot(data, ggplot2::aes(x = arm, y = age)) +", " ggplot2::geom_boxplot() +", " ggplot2::labs(x = NULL, y = \"Age (years)\")", "}" ), "tests/testthat/test-age-summary.R" = c( "test_that(\"print_age_summary() output is stable\", {", " expect_snapshot(print_age_summary(c(64, 71, 80)))", "})", "", "test_that(\"print_age_summary() explains a wrong input\", {", " expect_snapshot(print_age_summary(\"64\"), error = TRUE)", "})" ), "tests/testthat/test-age-plot.R" = c( "test_that(\"age_plot() looks the same\", {", " data <- data.frame(arm = rep(c(\"A\", \"B\"), each = 3), age = c(60, 65, 70, 62, 68, 74))", " vdiffr::expect_doppelganger(\"age-plot\", age_plot(data))", "})" ) ) # Creates the package and returns its path. make_demo_package <- function(path = file.path(tempdir(), "snapdemo")) { usethis::create_package(path, rstudio = FALSE, open = FALSE) usethis::with_project(path, { usethis::use_testthat(3) usethis::use_package("ggplot2") usethis::use_package("vdiffr", type = "Suggests") }) for (file in names(demo_files)) { writeLines(demo_files[[file]], file.path(path, file)) } path } # Runs the package's tests. `ci = FALSE` because this demo records new # snapshots on purpose: with CI=true, testthat fails a test that would create # a new snapshot (you forgot to commit it). run_demo_tests <- function( path, reporter = testthat::default_reporter(), ci = FALSE ) { withr::local_envvar(CI = tolower(as.character(ci))) testthat::test_local(path, reporter = reporter, stop_on_failure = FALSE) } # Changes the printed output: one decimal place becomes two. change_output <- function(path) { file <- file.path(path, "R", "age-summary.R") code <- readLines(file) writeLines(sub("%.1f (%.1f)", "%.2f (%.2f)", code, fixed = TRUE), file) } snapshot_files <- function(path) { list.files(file.path(path, "tests", "testthat", "_snaps"), recursive = TRUE) } # Run only when called as a script (not when sourced by the tests). if (sys.nframe() == 0L) { path <- make_demo_package() message("Run 1: records the snapshots") run_demo_tests(path) print(snapshot_files(path)) cat( readLines(file.path(path, "tests/testthat/_snaps/age-summary.md")), sep = "\n" ) message("Run 2: compares against them") run_demo_tests(path) message("Run 3: after changing the output") change_output(path) run_demo_tests(path) print(snapshot_files(path)) # the .new.md file holds the changed output }