-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Added Option to Automatically Add Vitest to New Projects using svelte-create #5708
Conversation
🦋 Changeset detectedLatest commit: 01ab4ba The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
packages/create-svelte/shared/+vitest+default/src/lib/Counter.test.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is it that requires +vitest+playwright+typescript
setup? Is there something in the Playwright config that needs to change?
packages/create-svelte/shared/+vitest-typescript/vitest.config.js
Outdated
Show resolved
Hide resolved
Do we want to add c8 + coverage script by default? |
Yea, this is true. Do you think I should just remove the coverage script as well? |
I'd skip it until we have an idea/documentation to actually build a full coverage test suite, which may need an additional util for mocking kit things as required. Eg. hooks, page-endpoints and maybe even some parts of .svelte files are very difficult to test right now. So a coverage report by c8 would be very inaccurate, either by giving a false sense of security when showing 100% even though it's not seeing everything, or by showing 50% and the user has no way of getting a higher coverage because tools are missing to implement tests. |
Removed coverage tests |
plugins: [sveltekit()], | ||
test: { | ||
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], | ||
globals: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another discussion ;) globals: true would mean we'd have to also provide additional env typing for the test files, just leave it off and resort to imports? It's cleaner anyways
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, just gonna admit that a lot of my decisions, have been based off what I've seen other people doing, and what my team has been doing.
I took a lot of inspiration from @davipon on his add vitest library
https://github.com/davipon/svelte-add-vitest/blob/main/templates/config/vite.config.js
I have yet to see someone not use globals I think because having to add them for every single test starts to get annoying, adding the imports just becomes repetitive boilerplate after a while. Definitely a preference though, the script already adds the vitest types to the tsconfig or Jsconfig based on what you choose.
THANKS @benmccann!!! If its a huge problem then I think we should just leave off globals for now as @dominikg said it's cleaner anyway. |
fixed double vite.config bug fixed type errors in game test
removed the vitest globals in favor of named imports as suggested by @dominikg while doing this I took the time to extensively test every configuration I found a bug adding two vite configs to every project which I fixed by removing the -vitest vite.config and renaming the +vitest vite.config.js to vite.config.ts @benmccann I also fixed a few type errors that broke the Game test that you should check out. I just had to add a default property to the constructor and split the string out in to a char array in the test. These work but, they were kind of quick fixes so feel free to update to something more your style. |
I don't recommend using vite.config.ts . The way vite creates a temporary vite.config.hash.js isn't great imho. |
If we suggest colocating test files, there should be documentation about how to handle + prefixed files. |
Updated and renamed vite.config.ts to vite.config.js and tested. |
unit test files are colocated in src, do we need to update tsconfig.json or show how that works at least so that users can have different ts settings for unit vs app code? or should the move it out of src in that case? I'd really love to avoid going down the multiple tsconfig files road but i'm not sure you can have both playwright and vitest .spec.ts files with different gobal needs from a single tsconfig |
@dominikg why would you need different TypeScript settings for unit vs app code? |
eg node env vs not |
Isn't that just defined once per config? If you want multiple, I think you have to have multiple configs regardless of what directories things live in? I don't have a ton of experience writing tests in TypeScript projects, so I'm not understanding what alternative is being proposed and if there's some best practice around this that you think this PR might be violating? |
it depends, for basic unit tests sharing the same tsconfig works. but I've seen multiple projects splitting it into a separate tsconfig.test.json with include and exclude. due to the way kit generates the root tsconfig that could become difficult. we can start with this setup but advanced usecases will require additional setup which isn't clear and there is no docs about it. |
is vitest-dev/vitest#2298 a blocker here? not supporting these makes some unit tests harder/ impossible related: #6259 |
I think we agreed to basic unit testing to start giving the ability to configure to their projects needs as it grows. This issue seems semi problematic but, could we not use mocks to get around this? I know in the issue the they use mockImport() which automatically makes a mock but couldn't for now they just use .mock() and do it by hand does that not work? |
const config = { | ||
plugins: [sveltekit()], | ||
test: { | ||
include: ['src/**/*.{test,spec}.{js,ts}'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One late note since I saw the topic of globals was already discussed:
Some libraries enable features automatically based on these globals and users coming from jest may be confused as to why things aren’t working the same way out of the box. One that got me was Testing Library, which enables auto cleanup only if the global is present. https://testing-library.com/docs/react-testing-library/api#cleanup (this is a link to react docs but it applies equally to the svelte version of the library)
I personally have globals enabled but I have a second tsconfig to add the global types to my testing folder, which I saw you are trying to avoid. I don’t think there’s one true answer here but just wanted to leave this note in case it benefits anyone.
thanks all! very happy to get this in |
Thanks everyone!!! |
Issue
Getting started with unit testing in Sveltekit can be confusing, which was brought up in this issue here #4423
Resolution
Testing
Possible problems
$app/env
and$app/stores
I might need input from @benmccann on this one...