-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Svelte 5: Simplify client-side API #9827
Comments
(Bonus: if we do this, |
IIRC you brought The reason why we introduced I don't fully understand why having |
We definitely don't need I actually think we should have two APIs:
I don't think you should be able to set props on a mounted component. It's just not needed and feels very non-declarative. If you want to change what gets passed in, you just change the state directly: const props = $state({ });
mount(App, { props, target: document.body });
props.foo = 'something else'; |
Imperative control over the component is important for testing, so we shouldn't remove it IMO. How else would you test that the component does what you want? |
@dummdidumm Simple: const props = $state({ });
mount(App, { props, target: document.body }); |
That would require people to also preprocess their test files before passing them to whatever - is that doable? cc @dominikg What if you're in an environment where you only interact with / consume the end result, and don't want to add the Svelte compiler to your tool chain? |
Couple more thoughts:
// tests/test.ts
import { mount, flush } from 'svelte';
import { it, assert } from 'vitest';
import { createProps } from './utils.svelte.js';
import Component from './Component.svelte';
it('works', async () => {
const props = createProps({ name: 'world' });
const target = document.createElement('div');
const [unmount, exports] = await mount(App, { props, target });
assert.equal(target.innerHTML, '<h1>hello world!</h1>');
props.name = 'everybody';
flush();
assert.equal(target.innerHTML, '<h1>hello everybody!</h1>');
unmount();
}); // tests/utils.svelte.js
export function createProps(data) {
const props = $state(data);
return props;
} |
@Rich-Harris We don't have a As for making them async – this makes sense to me. |
Sorry, that was confusing — that was partly a follow-up to a whole separate conversation in Discord, about making the hydration library code treeshakeable |
One thing against the |
@dummdidumm what exactly do you want to preprocess? the .svelte file or the test definition? ultimately the best dx would be to have it encapsulated in testing-library/svelte. requiring a custom bundler plugin or svelte preprocessor doesn't sound great. esp the component code under test must not be modified in any way that could change its behavior from production code... sourcemaps and step debugging tests could also be affected, so ideally we don't need custom transforms by default. opt-in transforms that reduce boilerplate, maaaaybe |
* feat: add hydrate method, make hydration treeshakeable Introduces a new `hydrate` method which does hydration. Refactors code so that hydration-related code is treeshaken out when not using that method. closes #9533 part of #9827 * get docs building * ugh * one more * Update packages/svelte/scripts/check-treeshakeability.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * warn * Update sites/svelte-5-preview/src/routes/docs/content/01-api/05-functions.md --------- Co-authored-by: Rich Harris <richard.a.harris@gmail.com> Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Rich Harris <rich.harris@vercel.com>
…ce `unmount` (#10516) * breaking: remove `createRoot`, adjust `mount`/`hydrate` APIs, introduce `unmount` closes #9827 * Update packages/svelte/src/internal/client/runtime.js Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> --------- Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
Hey sorry to butt in on a closed thread but I used this example (and code from the Svelte codebase) as a simple way of writing a component test without the need for something like testing-library but now I'm getting this error: Here's my test:
But I still see this pattern being used in the source code. I'd love to be able to have a stable/simple way of rendering components for unit tests. Any recommendations for how I can either
|
Describe the problem
At the moment we have a pair of mildly confusing APIs for creating components —
createRoot
andmount
. If I'm completely honest I can't remember what the difference is and why we have both. I'm sure there's a good reason, I just don't care. It's too complicated.One thing I do know is that the
createRoot
function adds$set
and$destroy
methods, similar to the ones we have on legacy classes. This means that you can do this sort of thing:Honestly, it's all a bit weird and ugly. On top of that, the
app.count
getter/setter only exists if you compile with theaccessors: true
option, which causes a bunch of extra code to be generated.Describe the proposed solution
Let's do this:
Or, for components where you don't need to update the props from outside after the component has been created:
Here,
createProps
is just a wrapper around the internalproxy
function (or maybe just a re-export, for that matter?) There's nocreateRoot
, no$set
, and no$destroy
(except for the return value frommount
, mirroring the$effect.root(...)
signature. Props behave exactly like they do in runes mode.accessors
is unnecessary, and therefore deprecated.Alternatives considered
n/a
Importance
nice to have
The text was updated successfully, but these errors were encountered: