Skip to content

expect_snapshot json and json2 failing for a simple reason #1309

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

Closed
franzbischoff opened this issue Jan 26, 2021 · 2 comments
Closed

expect_snapshot json and json2 failing for a simple reason #1309

franzbischoff opened this issue Jan 26, 2021 · 2 comments
Labels
bug an unexpected problem or unintended behavior snapshot 📷

Comments

@franzbischoff
Copy link

Hello,

I'm forced to use the option serialize (which is not 100% portable) because of this problem on json's:

Both uses jsonlite library, and uses the default argument digits.

This causes the following problem:

Error (test-windowfunc.R:64:5): mov_sum
Error: Serialization round-trip is not symmetric.

     value         | roundtrip
 [1] 5.7254281461  - 5.7254281500  [1]
 [2] 5.7587528272  - 5.7587528300  [2]
 [3] 5.2088458248  - 5.2088458200  [3]
 [4] 4.1113388848  - 4.1113388800  [4]
 [5] 2.6462555057  - 2.6462555100  [5]
 [6] 0.8732967544  - 0.8732967500  [6]
 [7] -1.0435459914 - -1.0435459900 [7]
 [8] -2.8678391535 - -2.8678391500 [8]
 [9] -4.3692381299 - -4.3692381300 [9]
[10] -5.4659227005 - -5.4659227000 [10]
 ... ...             ...           and 716 more ...

This could be fixed using some tolerance, right? :-)

Best regards

@hadley
Copy link
Member

hadley commented Feb 7, 2021

Can you please provide a minimal reprex (reproducible example)? The goal of a reprex is to make it as easy as possible for me to recreate your problem so that I can fix it: please help me help you!

If you've never heard of a reprex before, start by reading "What is a reprex", and follow the advice further down the page. Please make sure your reprex is created with the reprex package as it gives nicely formatted output and avoids a number of common pitfalls.

@hadley hadley added the reprex needs a minimal reproducible example label Feb 7, 2021
@nlarusstone
Copy link

I have the same issue, here's a simple reprex:

f <- function() {
  0.900000000000001
}

test_that("Saving/loading json works", {
  expect_snapshot_value(f(), style="json2")
})
#> Error in test_that("Saving/loading json works", Error: Serialization round-trip is not symmetric.

Created on 2021-02-24 by the reprex package (v1.0.0)

and test output:

Error (test-test.R:6:3): Saving/loading json works
Error: Serialization round-trip is not symmetric.

    `value`: 0.900000000000001
`roundtrip`: 0.900000000000000

i You may need to consider serialization `style`
Backtrace:
 1. testthat::expect_snapshot_value(f(), style = "json2") test-test.R:6:2
 2. testthat:::expect_snapshot_helper(...)
 3. snapshotter$take_snapshot(val, save = save, load = load, ...)
 4. testthat:::check_roundtrip(value, load(save(value)))

Seems like the issue is that jsonlite::serializeJSON has a default argument of truncating to 8 digits (https://github.com/jeroen/jsonlite/blob/master/R/serializeJSON.R), so load(save(v)) ends up being different from v (https://github.com/r-lib/testthat/blob/master/R/snapshot-reporter.R#L68)

@hadley hadley added bug an unexpected problem or unintended behavior snapshot 📷 and removed reprex needs a minimal reproducible example labels Jul 8, 2021
@hadley hadley closed this as completed in 79a0258 Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior snapshot 📷
Projects
None yet
Development

No branches or pull requests

3 participants