Skip to content

Broken snapshot causes subsequent snapshots within same test block to be auto-deleted #168

Open

Description

@dmuenz

Say I have a test_that block with two expect_doppelganger snapshots, and both snapshots are fine. Then I break the first snapshot so that the plotting code errors. When I run the tests, the first expectation will fail and the second expectation will not get run and thus its associated snapshot file will get auto-deleted. I was not expecting the second snapshot to have its SVG file auto-deleted and I panicked when I first noticed this behavior.

A workaround to this problem is to put every expect_doppelganger in its own test_that block. Then if one breaks, the others will still run. Consider the following test file, which runs three expect_doppelganger snapshots split into two test_that blocks:

f <- function(succeed = TRUE) {
  if (succeed)
    ggplot2::ggplot()
  else
    stop("Failure!")
}

test_that("test block 1", {
  vdiffr::expect_doppelganger("plot-1", f(succeed = TRUE))
  vdiffr::expect_doppelganger("plot-2", f(succeed = TRUE))
})

test_that("test block 2", {
  vdiffr::expect_doppelganger("plot-3", f(succeed = TRUE))
})

If you run this test file, you get three saved snaps: plot-1.svg, plot-2.svg, plot-3.svg. Now break just the "plot-1" expectation by changing TRUE to FALSE, and then re-run the test file. Since the "plot-1" expectation is broken, the plot-2.svg file gets auto-deleted. But since the "plot-3" expectation is in its own block, we don't lose the plot-3.svg file.

There was a closely related issue reported in r-lib/testthat#1393. There the issue, translating to my own example, was that the plot-1.svg file was getting deleted. The issue was fixed by automatically "announcing" the snapshot file before evaluation. But that does not fix my issue of a broken snapshot preventing a subsequent snapshot in the same block from being run.

Could vdiffr somehow announce all the snapshots within a block upfront? If not, I think the current behavior should be documented, possibly with the recommendation to put every expect_doppelganger in its own test_that block. Thanks for your consideration.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions