Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error parameter for expect_snapshot() #1200

Closed
lionel- opened this issue Oct 14, 2020 · 2 comments · Fixed by #1261
Closed

error parameter for expect_snapshot() #1200

lionel- opened this issue Oct 14, 2020 · 2 comments · Fixed by #1261
Labels
feature a feature request or enhancement snapshot 📷

Comments

@lionel-
Copy link
Member

lionel- commented Oct 14, 2020

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.

@lionel-
Copy link
Member Author

lionel- commented Dec 16, 2020

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.

@lionel-
Copy link
Member Author

lionel- commented Dec 16, 2020

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
Labels
feature a feature request or enhancement snapshot 📷
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants