-
When testing my atom graph, I want to pre-set some async atoms to a resolved value and observe the loadable atoms that wrap them. This is what my test fixture looks like: test('loadable', () => {
const store = createStore()
const asyncAtom = atom(Promise.resolve('resolved'))
const loadableAtom = loadable(asyncAtom)
renderHook(() => useAtomValue(loadableAtom), {
wrapper: props => <Provider {...props} store={store} />
})
// Assertion fails, state is 'loading'
expect(store.get(loadableAtom).state).toBe('hasData')
}) As you can see, the assertion fails as the loadable always starts in in the Things I've tried, without success:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Promise will be evaluated when the atom is first read. So, I think you have to await after the first read. expect(store.get(loadableAtom).state).toBe('loading')
await Promise.resovle() // wait for a microtask
expect(store.get(loadableAtom).state).toBe('hasData') Hm, but Would you be able to create a reproduction with StackBlitz? |
Beta Was this translation helpful? Give feedback.
This worked thanks! However I now had three warnings regarding
act
:This is solved by wrapping the render into an awaited
act
, which also deals with awaiting all microtasks:Sandbox: https://stackblitz.com/edit/vitest-dev-vitest-e…