Replies: 1 comment
-
|
Update: A rather clean approach to this problem, without needing any change from the library: We need a utility component: <!-- query-wrapper.svelte -->
<script lang="ts">
import { QueryClient, setQueryClientContext } from "@tanstack/svelte-query";
const { children } = $props();
const queryClient = new QueryClient();
setQueryClientContext(queryClient);
</script>
{@render children?.()}Then we render this in the test script import QueryWrapper from "$lib/test-helpers/query-wrapper.svelte"
describe("Component.svelte", () => {
it("should render", async () => {
render(QueryWrapper, {
props: {
children: Component
}
});
const text = page.getByTestId("foo");
await expect.element(heading).toBeInTheDocument();
});
});This would work, though I suppose exposing the context key like how I suggested above is a bit cleaner. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Given a Svelte component that uses
svelte-query.I would like to do Component testing with Vitest
However an error is expected:
In React-land, we can wrap the component-under-test with
QueryClientProviderBut this cannot be done with Svelte, as there would be no concept of JSX.
Available literature online for this issue is practically non-existent or not up to date. For example, some suggests using
svelte-htmorsvelte-jsxto do this, but the libraries are long-abandoned and is not expected to work with Svelte 5.Suggested approach
The library should export the context key:
query/packages/svelte-query/src/context.ts
Lines 1 to 6 in bf7f47e
This way, we can inject the
queryClientas a render context.Beta Was this translation helpful? Give feedback.
All reactions