Snapshot testing for data.frames

Description

copied from https://github.com/bcgov/ssdtools with permission (https://github.com/bcgov/ssdtools/issues/379)

Usage

expect_snapshot_data(x, name, digits = 6, ...)

Arguments

x a data.frame to snapshot
name character snapshot name
digits integer passed to signif() for numeric variables

Arguments passed on to testthat::expect_snapshot_file

binary
[Deprecated] Please use the compare argument instead.
cran
Should these expectations be verified on CRAN? By default, they are not, because snapshot tests tend to be fragile because they often rely on minor details of dependencies.
transform
Optionally, a function to scrub sensitive or stochastic text from the output. Should take a character vector of lines as input and return a modified character vector as output.
variant

If not-NULL, results will be saved in _snaps/{variant}/{test}/{name}. This allows you to create different snapshots for different scenarios, like different operating systems or different R versions.

Note that there’s no way to declare all possible variants up front which means that as soon as you start using variants, you are responsible for deleting snapshot variants that are no longer used. (testthat will still delete all variants if you delete the test.)
old,new
Paths to old and new snapshot files.

Value

NULL (from testthat::expect_snapshot_file())

Examples

Code
library("snapr")


# expect_snapshot_data() must be called inside a test_that() block with
# testthat 3rd edition active. Outside a test suite, the snapshot is
# skipped because there is no reference file to compare against.
withr::with_tempdir({
  testthat::test_that("iris snapshot", {
    testthat::local_edition(3)
    expect_snapshot_data(iris, name = "iris")
  })
})
── Snapshot ────────────────────────────────────────────────────────────────────
/tmp/RtmpNgLb83/file1b3b382acf70.csv
────────────────────────────────────────────────────────────────────────────────
── Skip: iris snapshot ─────────────────────────────────────────────────────────
Reason: empty test