useAsyncData in tests is very, very annoying #1059
TheDutchCoder
started this conversation in
General
Replies: 1 comment 1 reply
-
Hello @TheDutchCoder you can try using testing-library. It brings great async utilities including //Trigger the action
const thing = screen.getByTestId('thing'); // Assuming `thing` has a `data-testid`
await fireEvent.click(thing);
// Wait for the DOM to reflect the update
await waitFor(() => {
const otherThing = screen.getByTestId('other-thing'); // Use data-testid
expect(otherThing.innerHTML).toBe('2');
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It has taken me well over a day to figure out that when using
useAsyncData
in a component, and you need to test its reactivity (i.e. change a query param and wait for new results to show in the DOM), then you need to use the following:The fact that there's no "native" way to deal with this is frustrating, and it seems like a crucial and very basic part of Nuxt to have coverage for? I had to figure this out by pure luck (in combination with using
registerEndpoint
to deal with the actual calls, for which the docs are very, very minimal).Can we get better support for this?
Things work fairly well/predictable if you depend on directly using
$fetch
, but in the vast majority of cases people will use the reactive composable (because that's what the docs tell us to use) for data fetching.Beta Was this translation helpful? Give feedback.
All reactions