Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ssssota/svelte-jsx-snippet

Write JSX as Svelte snippets for testing.
https://github.com/ssssota/svelte-jsx-snippet

jsx svelte testing

Last synced: 9 days ago
JSON representation

Write JSX as Svelte snippets for testing.

Awesome Lists containing this project

README

        

# svelte-jsx-snippet

Write JSX as Svelte snippets for testing.

## Setup

```sh
npm install -D svelte-jsx-snippet
```

tsconfig.json

```jsonc
{
"compilerOptions": {
// set jsx config
"jsx": "preserve",
"jsxImportSource": "svelte-jsx-snippet",
},
// include jsx/tsx files
"include": ["**/*.svelte", "**/*.ts", "**/*.tsx"],
}
```

vite.config.ts

```ts
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";
// Put svelteJsxSnippet plugin
import { svelteJsxSnippet } from "svelte-jsx-snippet/vite";

export default defineConfig({
plugins: [sveltekit(), svelteJsxSnippet()],
});
```

## Usage

For example: `MyComponent.svelte`

```svelte

const { children } = $props();

{@render children}
```

### For Storybook

```jsx
import MyComponent from "./MyComponent.svelte";
export default {
component: MyComponent,
};
export const Default = {
props: {
children:

Hello, World!

,
},
};
```

### For Vitest

```jsx
import { test, expect } from "vitest";
import { render } from "@testing-library/svelte";
import MyComponent from "./MyComponent.svelte";
test("render snippet ", () => {
const { getByText } = render(MyComponent, {
props: {
children:

Hello, World!

,
},
});
expect(getByText("Hello, World!")).toBeInTheDocument();
});
```

## Constraints

- Support only Svelte5
- because Snippet is a feature of Svelte5.
- You can't use Svelte's special syntax.
- For example, `#if`, `#each`, `#await`, etc.

## License

MIT