Skip to content

error parameter for expect_snapshot() #1200

Closed

Description

@lionel-

It might be useful to add an error = FALSE argument to expect_snapshot(). When TRUE, no error is a failure. When FALSE, error is a failure. NULL would correspond to the current behaviour (no failure either way). When non-null, presence or absence of error would be checked on CRAN even when cran = FALSE.

Activity

  1. added a commit that references this issue on Dec 2, 2020
    26355b7
  2. added a commit that references this issue on Dec 4, 2020
    aadddba
  3. lionel- commented on Dec 16, 2020

    @lionel-
    MemberAuthor

    Just a note that multiple errors can be flexibly and conveniently tested in this way:

    cnd_cat <- function(x) {
      cat(paste0(conditionMessage(x), "\n"))
    }
    
    test_that("%|% fails with wrong types", {
      expect_snapshot({
        cnd_cat(expect_error(c(1L, NA) %|% 2))
        cnd_cat(expect_error(c(1, NA) %|% ""))
        cnd_cat(expect_error(c(1, NA) %|% call("fn")))
      })
    })

    This prints both the code and the error message in the snapshot file. expect_snapshot() fails if an expected error does not occur.

  4. lionel- commented on Dec 16, 2020

    @lionel-
    MemberAuthor

    Easier way suggested by @hadley:

    test_that("%|% fails with wrong types", {
      expect_snapshot({
        (expect_error(c(1L, NA) %|% 2))
        (expect_error(c(1, NA) %|% ""))
        (expect_error(c(1, NA) %|% call("fn")))
      })
    })

    This prints the condition objects returned invisibly by expect_error(), including the error message.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions