Skip to content

Reuse value generated by arbitrary multiple times #3074

Answered by dubzzz
mrmarbury asked this question in Q&A
Discussion options

You must be logged in to vote

Option 1: not my recommendation as chain tend to shrink badly

export const buildArbitraryObject = (): fc.Arbitrary<MyType> => {
  return fc.string().chain(s => fc.record<MyType>({
    key: s,
    value: fc.record<SubType>({
      something: s
    })
  })),
}

Option 2: with map

export const buildArbitraryObject = (): fc.Arbitrary<MyType> => {
  return fc.record({
    key: s,
    value: fc.record({ // value without the something part
    })
  })).map(o => ({...o, value: {...o.value, something: o.key}})),
}

Option 3: I have to think a way to simplify the construction of such arbitrary to have something as simple as chain version (or even simpler) but with the shrink capacities of map

Disclai…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mrmarbury
Comment options

Answer selected by mrmarbury
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants