question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow deterministic snapshot testing

See original GitHub issue

🚀 Feature Request

Add a function that uses arbitraries to deterministically create output.

Motivation

When working with legacy code it is often hard to define exact properties. The main goal usually is to “keep the existing behavior”. Nonetheless, property-based testing can help here by generating random inputs for a fixed seed and returning input-output pairs. These can then be saved and compared with help of snapshot testing tools, e. g. Jest Snapshot testing.

Example

This could be implemented as

interface IOutput<T1, R> {
  inputs: [T1],
  output: R
}

function generateResults<T1, R>(
        arb1: Arbitrary<T1>,
        resultGetter: (t1:T1) => R): IOutput<T1, R>[];

and then used in jest as

function add(a, b) {
  return a+b
}

...

it('returns the same results as before', () => {
  const results = fc.generateResults(
    fc.float(),
    fc.float(),
    add
  )
  expect(results).toMatchSnapshot()
})

Open questions

  • How to handle seeding? The output needs to be deterministic (otherwise the snapshot will fail all the time). My preferred solution would be to just add a global option for a seed that by default is 0, but I am not sure how fast-check behaves with a fixed seed 0.
  • I would be willing to try myself at a PR, if there is interest in this, but haven’t worked with the internals of the library before, so I might need some pointers where to start.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dubzzzcommented, Jan 24, 2020

@dbartholomae

For your information:

The next minor version of fast-check - 1.22.0 - will certainly change the way fast-check generates random values. Up to 1.21.0 random instances generated by fast-check were too close - see https://github.com/dubzzz/fast-check/issues/524. I had to change the way fast-check uses random generators to fix this issue.

You might need to update your snapshots.

0reactions
dbartholomaecommented, Dec 19, 2019

D’oh, I should have seen that xD Well, I’ll stick to sample then for now 😃 Is there any other feature you would like me help with? Just tag me in the relevant issue, I have some spare time between the years.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Snapshot Testing
Your tests should be deterministic. Running the same tests multiple times on a component that has not changed should produce the same results ......
Read more >
java-snapshot-testing/README.md at master
You need to ensure your test is deterministic for all fields (there are ways to ignore things like dates); Does not give great...
Read more >
Using Jest Mocks to Prevent Non-Deterministic or ...
Snapshot testing is great. I work with React a lot and frequently use Storybook to do visual and snapshot testing of my components....
Read more >
Snapshot Testing with Jest - Bambielli's Blog
Snapshot testing is a response to visual regression testing by Facebook, and was released in July of 2016 with Jest v14.0. Instead of...
Read more >
Snapshot Testing - Jest
Snapshot Testing · 1. Treat snapshots as code · 2. Tests should be deterministic · 3. Use descriptive snapshot names.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found