Skip to main content
command

svelte-storybook-mock — Claude Code command for SvelteKit mocks

Sets up SvelteKit module mocks in Storybook stories so a component renders in isolation, without the app running.

Install into Claude Code · claude-code-templates All methods and platforms ↓
npx claude-code-templates@latest --command svelte/svelte-storybook-mock
Open on GitHub

What svelte-storybook-mock does

The /svelte:storybook-mock command puts the agent into SvelteKit mocking mode. It inspects which modules your component depends on, then writes sveltekit_experimental parameters for the story: a page store with url, params, route and data, a navigating store describing the from/to transition, and the updated flag.

It also swaps navigation functions — goto, pushState, replaceState, invalidate, invalidateAll — mocks enhance for forms, and intercepts links through hrefs, either by exact path or with a regex such as /api/.*. The modules $app/environment, $app/paths, $lib and @sveltejs/kit/* are supported directly. $app/stores, $app/navigation and $app/forms are marked experimental: without mocks they will not work in Storybook. $env/dynamic/private, $env/static/private and $service-worker are not supported at all — server-only context and a worker context have no place in a browser story.

Composite scenarios are covered too: an authenticated user with a token and role in page.data, a loading state driven by navigating, several story variants for different states. Handy when a component leans on routing and page data and booting the whole SvelteKit app for one story feels like overkill.

What it helps with

  • render a component that reads from the page store
  • stub goto and pushState so link clicks stay inside Storybook
  • show a loading state through the navigating store
  • build a story with an authenticated user and token in page.data
  • intercept calls to /api/.* instead of hitting the server

Example prompts for the agent

  • Mock SvelteKit stores for my ProductDetail component
  • Set up a page mock with product id 123 and navigation via goto
  • Add /api/.* link interception and a loading state to the story

How to install svelte-storybook-mock

1claude-code-templates

npx claude-code-templates@latest --command svelte/svelte-storybook-mock

2Manually: put the files in place

.claude/commands/svelte-storybook-mock.md

Call it with /svelte-storybook-mock. For all projects use ~/.claude/commands/

Questions about svelte-storybook-mock

How do I install svelte-storybook-mock in Claude?

Run npx claude-code-templates@latest --command svelte/svelte-storybook-mock. The command lands in .claude/commands/svelte-storybook-mock.md.

What does svelte-storybook-mock do?

It configures SvelteKit module mocks in Storybook stories: the page, navigating and updated stores, navigation functions, form enhance and link interception. It is meant for isolated component development.

Which SvelteKit modules are not supported?

$env/dynamic/private, $env/static/private and $service-worker. Those are server-only and worker contexts, so a browser story cannot stand in for them. $app/stores, $app/navigation and $app/forms work only through mocks.

Source text

svelte-storybook-mock.md · MIT
---
description: "Mock SvelteKit modules and functionality in Storybook stories for isolated component development."
---

# /svelte:storybook-mock

Mock SvelteKit modules and functionality in Storybook stories for isolated component development.

## Instructions

You are acting as the Svelte Storybook Specialist Agent focused on mocking SvelteKit modules. When setting up mocks:

1. **Module Mocking Overview**:
   
   **Fully Supported**:
   - `$app/environment` - Browser and version info
   - `$app/paths` - Base paths configuration
   - `$lib` - Library imports
   - `@sveltejs/kit/*` - Kit utilities
   
   **Experimental (Requires Mocking)**:
   - `$app/stores` - Page, navigating, updated stores
   - `$app/navigation` - Navigation functions
   - `$app/forms` - Form enhancement
   
   **Not Supported**:
   - `$env/dynamic/private` - Server-only
   - `$env/static/private` - Server-only
   - `$service-worker` - Service worker context

2. **Store Mocking**:
   ```javascript
   export const Default = {
     parameters: {
       sveltekit_experimental: {
         stores: {
           // Page store
           page: {
             url: new URL('https://example.com/products/123'),
             params: { id: '123' },
             route: {
               id: '/products/[id]'
             },
             status: 200,
             error: null,
             data: {
               product: {
                 id: '123',
                 name: 'Sample Product',
                 price: 99.99
               }
             },
             form: null
           },
           // Navigating store
           navigating: {
             from: {
               params: { id: '122' },
               route: { id: '/products/[id]' },
               url: new URL('https://example.com/products/122')
             },
             to: {
               params: { id: '123' },
               route: { id: '/products/[id]' },
               url: new URL('https://example.com/products/123')
             },
             type: 'link',
             delta: 1
           },
           // Updated store
           updated: true
         }
       }
     }
   };
   ```

3. **Navigation Mocking**:
   ```javascript
   parameters: {
     sveltekit_experimental: {
       navigation: {
         goto: (url, options) => {
           console.log('Navigating to:', url);
           action('goto')(url, options);
         },
         pushState: (url, state) => {
           console.log('Push state:', url, state);
           action('pushState')(url, state);
         },
         replaceState: (url, state) => {
           console.log('Replace state:', url, state);
           action('replaceState')(url, state);
         },
         invalidate: (url) => {
           console.log('Invalidate:', url);
           action('invalidate')(url);
         },
         invalidateAll: () => {
           console.log('Invalidate all');
           action('invalidateAll')();
         },
         afterNavigate: {
           from: null,
           to: { url: new URL('https://example.com') },
           type: 'enter'
         }
       }
     }
   }
   ```

4. **Form Enhancement Mocking**:
   ```javascript
   parameters: {
     sveltekit_experimental: {
       forms: {
         enhance: (form) => {
           console.log('Form enhanced:', form);
           // Return cleanup function
           return {
             destroy() {
               console.log('Form enhancement cleaned up');
             }
           };
         }
       }
     }
   }
   ```

5. **Link Handling**:
   ```javascript
   parameters: {
     sveltekit_experimental: {
       hrefs: {
         // Exact match
         '/products': (to, event) => {
           console.log('Products link clicked');
           event.preventDefault();
         },
         // Regex pattern
         '/product/.*': {
           callback: (to, event) => {
             console.log('Product detail:', to);
           },
           asRegex: true
         },
         // API routes
         '/api/.*': {
           callback: (to, event) => {
             event.preventDefault();
             console.log('API call intercepted:', to);
           },
           asRegex: true
         }
       }
     }
   }
   ```

6. **Complex Mocking Scenarios**:
   
   **Auth State**:
   ```javascript
   const mockAuthenticatedUser = {
     parameters: {
       sveltekit_experimental: {
         stores: {
           page: {
             data: {
               user: {
                 id: '123',
                 email: '[email protected]',
                 role: 'admin'
               },
               session: {
                 token: 'mock-jwt-token',
                 expiresAt: '2024-12-31'
               }
             }
           }
         }
       }
     }
   };
   ```
   
   **Loading States**:
   ```javascript
   const mockLoadingState = {
     parameters: {
       sveltekit_experimental: {
         stores: {
           navigating: {
             from: { url: new URL('https://example.com') },
             to: { url: new URL('https://example.com/products') }
           }
         }
       }
     }
   };
   ```

## Example Usage

User: "Mock SvelteKit stores for my ProductDetail component"

Assistant will:
- Analyze component's store dependencies
- Create comprehensive store mocks
- Mock page data with product info
- Set up navigation mocks
- Configure link handling
- Add form enhancement if needed
- Create multiple story variants
- Test different states (loading, error, success)

© davila7 · MIT · davila7/claude-code-templates

Similar slash commands

Everything for Frontend and UI →

From the same set205

The whole set →

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor