Skip to content

Commit 4512f89

Browse files
committed
Make new snapshots fail on CI
Fixes #1461
1 parent 13e102e commit 4512f89

File tree

7 files changed

+13
-12
lines changed

7 files changed

+13
-12
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# testthat (development version)
22

3+
* `expect_snapshot()` and friends will now fail when creating a new snapshot on CI. This is usually a signal that you've forgotten to run it locally before committing (#1461).
34
* `expect_snapshot_value()` can now handle expressions that generate `-` (#1678) or zero length atomic vectors (#2042).
45
* `expect_matches()` failures should be a little easier to read (#2135).
56
* New `local_on_cran(TRUE)` allows you to simulate how your tests will run on CRAN (#2112).

R/test-files.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ test_files_reporter <- function(reporter, .env = parent.frame()) {
311311
reporters <- list(
312312
find_reporter(reporter),
313313
lister, # track data
314-
local_snapshotter("_snaps", fail_on_new = FALSE, .env = .env)
314+
local_snapshotter("_snaps", fail_on_new = on_ci(), .env = .env)
315315
)
316316
list(
317317
multi = MultiReporter$new(reporters = compact(reporters)),

tests/testthat/test-snapshot-file.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ test_that("expect_snapshot_file works with variant", {
5454
})
5555

5656
test_that("basic workflow", {
57-
snapper <- local_snapshotter()
57+
snapper <- local_snapshotter(fail_on_new = FALSE)
5858

5959
# warns on first run
6060
snapper$start_file("snapshot-6", "test")
@@ -79,7 +79,7 @@ test_that("basic workflow", {
7979
})
8080

8181
test_that("can announce snapshot file", {
82-
snapper <- local_snapshotter()
82+
snapper <- local_snapshotter(fail_on_new = FALSE)
8383
snapper$start_file("snapshot-announce", "test")
8484
announce_snapshot_file(name = "bar.svg")
8585
expect_equal(snapper$snap_file_seen, "snapshot-announce/bar.svg")

tests/testthat/test-snapshot-reporter.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
test_that("can establish local snapshotter for testing", {
2-
snapper <- local_snapshotter()
2+
snapper <- local_snapshotter(fail_on_new = FALSE)
33

44
snapper$start_file("snapshot-1", "test")
55
expect_true(snapper$is_active())
@@ -9,7 +9,7 @@ test_that("can establish local snapshotter for testing", {
99

1010
test_that("basic workflow", {
1111
path <- withr::local_tempdir()
12-
snapper <- local_snapshotter(path)
12+
snapper <- local_snapshotter(path, fail_on_new = FALSE)
1313
snapper$start_file("snapshot-2")
1414
# output if not active (because test not set here)
1515
expect_snapshot_output("x") |>
@@ -39,7 +39,7 @@ test_that("basic workflow", {
3939
})
4040

4141
test_that("only create new files for changed variants", {
42-
snapper <- local_snapshotter()
42+
snapper <- local_snapshotter(fail_on_new = FALSE)
4343
snapper$start_file("variants", "test")
4444
expect_warning(expect_snapshot_output("x"), "Adding new")
4545
expect_warning(expect_snapshot_output("x", variant = "a"), "Adding new")
@@ -75,7 +75,7 @@ test_that("only create new files for changed variants", {
7575
})
7676

7777
test_that("only reverting change in variant deletes .new", {
78-
snapper <- local_snapshotter()
78+
snapper <- local_snapshotter(fail_on_new = FALSE)
7979
snapper$start_file("v", "test")
8080
expect_warning(expect_snapshot_output("x", variant = "a"), "Adding new")
8181
expect_warning(expect_snapshot_output("x", variant = "b"), "Adding new")
@@ -98,7 +98,7 @@ test_that("only reverting change in variant deletes .new", {
9898

9999
test_that("removing tests removes snap file", {
100100
path <- withr::local_tempdir()
101-
snapper <- local_snapshotter(path)
101+
snapper <- local_snapshotter(path, fail_on_new = FALSE)
102102
snapper$start_file("snapshot-3", "test")
103103
expect_warning(expect_snapshot_output("x"), "Adding new")
104104
snapper$end_file()
@@ -110,7 +110,7 @@ test_that("removing tests removes snap file", {
110110
})
111111

112112
test_that("errors in test doesn't change snapshot", {
113-
snapper <- local_snapshotter()
113+
snapper <- local_snapshotter(fail_on_new = FALSE)
114114

115115
# First run
116116
snapper$start_file("snapshot-5", "test")

tests/testthat/test-snapshot-value.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test_that("reparse handles common cases", {
3232
})
3333

3434
test_that("errors if can't roundtrip", {
35-
snapper <- local_snapshotter()
35+
snapper <- local_snapshotter(fail_on_new = FALSE)
3636
snapper$start_file("snapshot-4", "test")
3737

3838
expect_error(expect_snapshot_value(NULL), "safely serialized")

vignettes/custom-expectation.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ library(testthat)
1212
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
1313
1414
# Pretend we're snapsotting
15-
snapper <- local_snapshotter()
15+
snapper <- local_snapshotter(fail_on_new = FALSE)
1616
snapper$start_file("snapshotting.Rmd", "test")
1717
```
1818

vignettes/snapshotting.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ library(testthat)
3636
```
3737

3838
```{r include = FALSE}
39-
snapper <- local_snapshotter()
39+
snapper <- local_snapshotter(fail_on_new = FALSE)
4040
snapper$start_file("snapshotting.Rmd", "test")
4141
```
4242

0 commit comments

Comments
 (0)