diff --git a/NEWS.md b/NEWS.md index d11b9fa8d..f882a8e25 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,6 +7,8 @@ ## Minor improvements and bug fixes +* Pass through warnings when `options(warn = 2)` is set (#721, @yutannihilation). + * `expect_lt()`, `expect_lte()`, `expect_gt()` `expect_gte()` now handle `Inf` and `NA` arguments appropriately (#732). diff --git a/R/test-that.R b/R/test-that.R index f3a732834..6f3769ead 100644 --- a/R/test-that.R +++ b/R/test-that.R @@ -122,6 +122,10 @@ test_code <- function(test, code, env = test_env(), skip_on_empty = TRUE) { invokeRestart("continue_test") } handle_warning <- function(e) { + # When options(warn) >= 2, a warning will be converted to an error. + # So, do not handle it here so that it will be handled by handle_error. + if (getOption("warn") >= 2) return() + handled <<- TRUE e$expectation_calls <- frame_calls(11, 5) register_expectation(e) diff --git a/tests/testthat/test-expect-error.R b/tests/testthat/test-expect-error.R index 1e30bb46f..ff754f941 100644 --- a/tests/testthat/test-expect-error.R +++ b/tests/testthat/test-expect-error.R @@ -50,3 +50,10 @@ test_that("generates informative failures", { expect_error(stop("xxx"), regexp = "zzz", class = "zzz") }) }) + +test_that("warnings are converted to errors when options('warn') >= 2", { + withr::with_options( + c(warn = 2), + expect_error(warning("foo")) + ) +})