-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
docs: Clarify how to run unit tests in a browser-like environment #14243
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
base: main
Are you sure you want to change the base?
Conversation
|
preview: https://svelte-dev-git-preview-svelte-14243-svelte.vercel.app/ this is an automated message |
see #14239 (comment) the use of jsdom should be limited |
Can you please explain in more details why using jsdom should be limited, and why it's not the right way? You made these assertions without explanations. Here's my reasoning for this PR: the docs already have a step to tell vitest to use the browser entry points, and having vitest use jsdom seems like a missed step rather than a new step. This is especially true because the docs have an example that doesn't work (using If your concern is with bringing the "browser version" of code of all packages into tests, the docs also already have a link to testing-library/svelte-testing-library#222 (comment) which should give people good pointers on how to separate those things. |
if you add See the example config i made here: https://github.com/dominikg/vitest-example-svelte5/blob/main/vitest.workspace.ts where there are 2 "vitest workspaces" for tests, one in a client environment with jsdom and svelte-testing-library, and the other a node environment without client addons. Only files with The template for adding vitest with |
Thank you! I understand better now the issue. I can do a bigger pass on this docs page to add this information, would that be ok, or is it a requirement to update the docs and |
the setup i mentioned earlier has been added to @DanielSidhion do you want to take a stab at it? |
@dominikg I can, but I'm currently on vacation until the first week of June, so it'd have to wait a bit before I get to this. If that's not ok, feel free to take over the PR, otherwise I should have something in a couple weeks. |
I'm back from vacation and watched the talk that @dominikg mentioned (here if you're curious). I'm planning to expand the unit test section with pretty much all the relevant content of the talk, and it seems like there's nothing to add regarding the E2E part of the docs when compared to the talk, so I'll keep the scope of this PR as-is. Making this comment just so folks are aware I'm working on it. btw, thanks for the talk @dominikg ! It was great. |
thanks!! Scott Spence did a great follow up blog post about it too. https://scottspence.com/posts/migrating-from-testing-library-svelte-to-vitest-browser-svelte it has a link to an examples repo as well. i think it's enough to describe the test setup here and then link to the sv vitest addon docs and scotts examples |
ea9e08a
to
6c58517
Compare
default setup from the svelte CLI.
6c58517
to
2ee724f
Compare
@dominikg I just pushed an initial attempt at updating the docs, would appreciate a review. |
Hey, thank you!! Scott Spence has been doing a lot of work on https://sveltest.dev and its far more comprehensive than the docs here could be. I think it would be best to limit this section to a very brief overview ala
and then link to sveltest.dev
The examples in my talk were very basic and did not follow best practices enough (i have regrets). I'd also leave out documentation of testing with jsdom, as we are in the process of changing the default to use vitest browser mode in |
@manuel3108 is there documentation for the sv vitest addon we can link here too? |
There is this: https://svelte.dev/docs/cli/vitest |
IMO it still feels important to keep jsdom. As an example, in a project I have a lot of tests for I don't mind the default from I can reorganise the sections and keep the jsdom stuff as an alternative. Do you think it's important to keep the "manual setup" section, or can we defer to |
Closes #14239.
The docs had some missing code when setting up vitest for testing in a browser-like environment, which this PR addresses. It also clarifies that this is needed for tests that use
$effect.root
.Given that a new svelte project (with vitest) created with the CLI doesn't change
vite.config.js
to include that browser-like environment config, it's easy to miss this extra config that is needed.