Skip to content

Testing resolved loadable atoms? #2597

Answered by franky47
franky47 asked this question in Q&A
Discussion options

You must be logged in to vote

This worked thanks! However I now had three warnings regarding act:

Warning: An update to TestComponent inside a test was not wrapped in act(...).

This is solved by wrapping the render into an awaited act, which also deals with awaiting all microtasks:

test('awaiting act', async () => {
  const store = createStore();
  const asyncAtom = atom(Promise.resolve('resolved'));
  const loadableAtom = loadable(asyncAtom);
  await act(async () => {
    renderHook(() => useAtomValue(loadableAtom), {
      wrapper: (props: any) => <Provider {...props} store={store} />,
    });
  });
  expect(store.get(loadableAtom).state).toBe('hasData');
});

Sandbox: https://stackblitz.com/edit/vitest-dev-vitest-e…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@franky47
Comment options

@dai-shi
Comment options

@franky47
Comment options

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