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

test_that desc with line breaks causes expect_snapshot to always be overwritten. #18

Open
LDSamson opened this issue Nov 15, 2023 · 6 comments
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@LDSamson
Copy link

Perhaps good to know that long descriptions in testthat::describe() or testthat::test_that(), as for example here, cause issues with testthat::expect_snapshot() for me. See the issue that I opened here.

@mjfrigaard
Copy link
Owner

Thank you for pointing this out! I will make a note of this in test tools.

BTW I started building this tiny package of BDD helpers that seems to be working (so far). I'll test it with snapshots 😃

@mjfrigaard mjfrigaard self-assigned this Nov 16, 2023
@mjfrigaard mjfrigaard added documentation Improvements or additions to documentation enhancement New feature or request labels Nov 16, 2023
@LDSamson
Copy link
Author

Note that I think it only occurs when there are line breaks in the description. Code like this still works, as far as I tested:

test_that(
  paste0(
    "A long description", 
    "over multiple lines"
  ), {
    expect_snapshot(2*4)
  }
)

@mjfrigaard
Copy link
Owner

I added a warning in test tools and included a shoutout 😃 . If you wrap the call to test_that() in a describe() it works:

describe(
    "Scenario: 
    Given 
    When 
    Then ",
    code = {
    test_that("fun()", code = {
      expect_snapshot(fun())
    })
  })

@LDSamson
Copy link
Author

Thank you for the shout out! Glad that the feedback was useful. Strange that it works when test_that() is wrapped inside describe(), since if you replace the test_that() with testthat::it() the problem occurs again. This would be a good workaround for now indeed, nice.

@LDSamson
Copy link
Author

Short update, since I checked the source code of expect_snapshot() (although you probably already noticed this):
the reason why this works:

describe(
  "Long
  description", 
  {
    test_that("test me", {expect_snapshot(2*2)})
  }
)

is because test_that() does not look for the description in describe() and will not add this description to the snapshot, while the function it() does . The problem is purely with the line breaks in the snapshot descriptions that are converted to \n characters.

I wrote a small pull request for the testthat package that should resolve the issue. Lets wait and see if the Posit team likes the solution!

@LDSamson
Copy link
Author

@mjfrigaard I saw that the small fix I proposed in testthat is approved and merged, so this problem should now be resolved in the latest testthat version! (version 3.2.2) https://github.com/r-lib/testthat/blob/main/NEWS.md r-lib/testthat#1902

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants