-
Is there a best practice or a good way to test code that has setup in a parent layout. For example, I used svelte kit latest sample and then tested a page that was using svelte query provider defined in the parent layout.
This fails with:
I'm also noticing other things like dynamic env's are causing issues too. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@niemyjski this is a very good question! Thanks for taking the time to post it. I lead a team maintaining a large SvelteKit app that uses svelte-query for its client-side API state management. This is a recommendation from me personally rather than anything "official," but I've found the best1 strategy for component tests is pretty classic Uncle Bob: separate the stuff you want to test away from the framework glue. In practice, this means:
For a simple, if slightly contrived example, if I wanted to test a user dashboard page's heading, I might write something like: <!-- dashboard/+page.svelte -->
<script>
import { createUserQuery } from '$lib/api'
import DashboardHeading from './dashboard-heading.svelte'
const userQuery = createUserQuery()
</script>
<DashboardHeading user={$userQuery.data} /> <!-- dashboard/dashboard-heading.svelte -->
<script>
import type { User } from '$lib/api';
const { user }: { user: User | undefined } = $props();
</script>
<h1>Hello {user?.name ?? 'friend'}!</h1> // dashboard-heading.spec.ts
// ...
it('renders level 1 heading with user name', () => {
render(DashboardHeading, { user: { name: 'Alice' } })
const heading = screen.getByRole('heading', { level: 1, name: /hello alice/iu })
expect(heading).toBeInTheDocument()
}) From there, I would also make sure that I had an E2E test for this page that also checked that there was a level-1 heading on the page matching. I've seen a number of guides online that recommend mocking out SvelteKit imports in your tests, but I definitely recommend your try to minimize or eliminate mocking in component tests. I'm a huge fan of mocking, but I've gone down the mock-heavy-component-tests path with React and RTL, and all it got me was a very brittle test suite that slowed my team down. Instead, I prefer to acknowledge that component tests are, by nature, highly integrated with code outside my control, and then do my best to minimize how many layers get tested in a given component test. Footnotes
|
Beta Was this translation helpful? Give feedback.
@niemyjski this is a very good question! Thanks for taking the time to post it.
I lead a team maintaining a large SvelteKit app that uses svelte-query for its client-side API state management. This is a recommendation from me personally rather than anything "official," but I've found the best1 strategy for component tests is pretty classic Uncle Bob: separate the stuff you want to test away from the framework glue. In practice, this means:
+page.svelte
/+layout.svelte
createQuery
,createMutation
, etc.