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

arguments from purrr lists not passed through to code documented in expect_snapshot() #1907

Closed
fh-mthomson opened this issue Nov 29, 2023 · 3 comments

Comments

@fh-mthomson
Copy link

test_that("wrong", {
  # only shows cli::cli_alert(.x) verbatim
  purrr::walk(
    c("a", "b"),
    ~expect_snapshot(cli::cli_alert(.x))
  )
})

test_that("right", {
  expect_snapshot(cli::cli_alert("a"))
  expect_snapshot(cli::cli_alert("b"))
})

Result:

── Warning ('test-mock.R:3:3'): wrong ──────────────────────────────────────────
Adding new snapshot:
Code
  cli::cli_alert(.x)
Message
  > a

── Warning ('test-mock.R:3:3'): wrong ──────────────────────────────────────────
Adding new snapshot:
Code
  cli::cli_alert(.x)
Message
  > b

── Warning ('test-mock.R:10:3'): right ─────────────────────────────────────────
Adding new snapshot:
Code
  cli::cli_alert("a")
Message
  > a

── Warning ('test-mock.R:11:3'): right ─────────────────────────────────────────
Adding new snapshot:
Code
  cli::cli_alert("b")
Message
  > b

@hadley
Copy link
Member

hadley commented Nov 29, 2023

You'd need to use rlang::inject() or similar for this:

library(testthat)

test_that("wrong", {
  local_edition(3)
  
  purrr::walk(
    letters[1:3],
    \(x) rlang::inject(expect_snapshot(message(!!x)))
  )
})
#> Can't compare snapshot to reference when testing interactively.
#> ℹ Run `devtools::test()` or `testthat::test_file()` to see changes.
#> Current value:
#> Code
#>   message("a")
#> Message
#>   a
#> Can't compare snapshot to reference when testing interactively.
#> ℹ Run `devtools::test()` or `testthat::test_file()` to see changes.
#> Current value:
#> Code
#>   message("b")
#> Message
#>   b
#> Can't compare snapshot to reference when testing interactively.
#> ℹ Run `devtools::test()` or `testthat::test_file()` to see changes.
#> Current value:
#> Code
#>   message("c")
#> Message
#>   c
#> ── Skip: wrong ─────────────────────────────────────────────────────────────────
#> Reason: empty test

Created on 2023-11-29 with reprex v2.0.2.9000

@hadley hadley closed this as completed Nov 29, 2023
@fh-mthomson
Copy link
Author

got it, thanks! It was confusing to me that it wasn't automatically passed through.

@hadley
Copy link
Member

hadley commented Nov 30, 2023

expect_snapshot() prints literally the code you typed, as if you had run it at the console. No automatic splicing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants