Skip to content

Best pactices for testing derived #347

Closed Answered by dai-shi
StephenStrickland asked this question in Q&A
Discussion options

You must be logged in to vote

The trick is valtio automatically batches multiple mutations and notification happens in async.
So, you need to test it in async:

await waitFor(() => {
  expect(derived.isTwo).toEqual(true)
})

Alternatively, you can disable the batching if that's what you'd expect:

    const derived = derive({
        isTwo: (snap) => snap(store).num === 2
    }, 
    {
        proxy: store,
        sync: true // to avoid batching
    })

Hope it helps.

Replies: 1 comment 1 reply

Comment options

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

Answer selected by StephenStrickland
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