diff --git a/NEWS.md b/NEWS.md index 6eb90a984..cf3fd1c36 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # testthat (development version) +* `test_that()` now emits an error if `expect_snapshot()` is invoked within + a `test_that()` case with an empty description (#1980; @kevinushey). + # testthat 3.2.1 * Fix incorrect format string detected by latest R-devel. Fix thanks to diff --git a/R/snapshot-reporter.R b/R/snapshot-reporter.R index f88040584..ebedcde65 100644 --- a/R/snapshot-reporter.R +++ b/R/snapshot-reporter.R @@ -46,6 +46,7 @@ SnapshotReporter <- R6::R6Class("SnapshotReporter", tolerance = testthat_tolerance(), variant = NULL, trace_env = NULL) { + check_string(self$test, allow_empty = FALSE) i <- self$new_snaps$append(self$test, variant, save(value)) old_raw <- self$old_snaps$get(self$test, variant, i) diff --git a/tests/testthat/test-snapshot.R b/tests/testthat/test-snapshot.R index ab35726b4..133f80288 100644 --- a/tests/testthat/test-snapshot.R +++ b/tests/testthat/test-snapshot.R @@ -162,3 +162,11 @@ test_that("hint is informative", { cat(snapshot_accept_hint("foo", "bar.R", reset_output = FALSE)) }) }) + +test_that("expect_snapshot requires a non-empty test label", { + + test_that("", { + expect_error(expect_snapshot(1 + 1)) + }) + +})