Proposed solution involves complex Svelte/Vite integration and unclear implementation details.
The issue requests inline Svelte components for testing, similar to JSX in React. The proposed solution involves a Vite plugin or Svelte compiler changes, but the implementation details and scope are unclear. This requires deep knowledge of Svelte and Vite, making it a high-difficulty task.
I'll start off by saying I don't know whether this would be a Svelte feature, or maybe part of a Vite plugin, or even if this should be in another library.
When I'm writing component tests using something like testing-library, I'll need to write some code like this (which requires jumping from file to file):
import {render} from '@testing-library/svelte'
import MyComponent from './MyComponent.svelte'
const result = render(MyComponent, componentOptions, renderOptions)
This leaves me jealous of frameworks like React where you can inline JSX and see a lot more of your component code (all of it, if you really wanted to):
import {render, screen} from '@testing-library/react'
import Fetch from './fetch'
test('loads and displays greeting', async () => {
render(<Fetch url="/greeting" />)
I'm attempting to port Tailwind's HeadlessUI-React library to Svelte 5, and in the process of porting, there are hundreds (thousands?) of inline JSX examples to test components in different ways, eg:
it('should be possible to render an (on) Switch using a render prop', () => {
render(
<Switch checked={true} onChange={console.log}>
{({ checked }) => <span>{checked ? 'On' : 'Off'}</span>}
</Switch>
)
...
Writing a custom Svelte file per test would be annoying, but moreso, just trying to debug that becomes a nightmare very quickly (I know, I've tried).
I have a partial solution that I'm kinda meh about, but think this might be a better first-party plugin or solution. Or maybe I can directly use the compiler or createRawSnippet to facilitate this, but I honestly am not sure about any of that.
Using a hacked together Vite plugin, I - on-the-fly - compile Svelte template strings into virtual modules, and
Claim this issue to let others know you're working on it. You'll earn 10 points when you complete it!