r-lib / testthat

An R šŸ“¦ to make testing šŸ˜€
https://testthat.r-lib.org
Other
888 stars 318 forks source link

snapshot test fails and requests edition3 when it is in package setup, fails as well if local_edition() is switched to edition3 #1935

Closed PierreENOlivier closed 5 months ago

PierreENOlivier commented 7 months ago

Hello, I have some snapshot tests failing in our company package. We use expect_snapshot() which requires edition3.

Edition3 is configured in the DESCRIPTION of the package and via usethis::use_testthat(edition = 3). I, thus, assume that the default edition in the package should be edition3. This version can be overridden locally using testthat::local_edition(2) which should only apply inside the test where it is called.

N.B. The following dummy tests are in the same file.

test_that('Generate binary matrix', {
  data <- 
    tibble(
      var1 = c(1, 0, 1, 0, 1),
      var2 = c(0, 0, 0, 1, 1),
      var3 = c(1, 0, 1, 1, 1)
    )

    # testthat::local_edition(3) # will fix the first test
    expect_snapshot(data)

})

test_that('Generate character matrix', {
  data_let <- 
    tibble(
      var1 = letters[1:5],
      var2 = letters[6:10],
      var3 = letters[11:15]
    )

    expect_snapshot(data_let)

})

Observations:

  1. If I click the "Run Tests" button on the file, it will generate the snapshot the first time and any following runs will succeed without errors.
  2. If I run all tests (Build > Test), then the same tests that previously succeeded will fail asking for local_edition(3) testthat:::edition_require(3, "expect_snapshot()").
  3. If I add the local_edition(3), the tests will fail saying that the snapshot has changed (though it should be the same). The only character that seems to have changed is the 'x' sign in 'A tibble: 5 x 3'. The old snapshot has a bold 'x' and the new snapshot has a normal font 'x'.

Is it a bug or something wrong with my config?

Thanks in advance,

hadley commented 5 months ago

That behaviour suggests to me that you're probably accidentally changing global state somewhere in your tests. You can use set_state_inspector() to help figure out what's going wrong.