r-lib / testthat

An R 📦 to make testing 😀
https://testthat.r-lib.org
Other
868 stars 313 forks source link

Mitigate linebreak issues in snapshot tests #1937

Open jennybc opened 3 months ago

jennybc commented 3 months ago

I periodically struggle with linebreak differences in snapshot tests and I've seen it come up in Slack a few times. Highlights of the situation:

Various ideas that might help:

gaborcsardi commented 3 months ago

The problem is the line breaks are calculated before the transform takes effect, so the output potentially still depends on how long the transformed value was.

There isn't much we can do about this, other than set

withr::local_options(cli.width = Inf)

to get rid of the line breaks.

In short, if you use cli and transform, then better use Inf width.

In theory expect_snapshot() could try to detect this situation, but I am not sure if it is worth it. We should definitely document it, though, possibly in the documentation of transform.

I'm not sure what's going on with width vs. cli.width, but I feel like there's something here.

cli.width takes precedence, and since testthat sets that (as well) to a fixed width, that's what you need to set as well.

jennybc commented 3 months ago

cli.width takes precedence, and since testthat sets that (as well) to a fixed width, that's what you need to set as well.

AHA! I have not come across that in the docs I've been reading, so either that's user error or something else that should be documented.