Skip to content
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

Merged
merged 24 commits into from
Nov 26, 2022

Conversation

bertybot
Copy link
Contributor

Issue

Getting started with unit testing in Sveltekit can be confusing, which was brought up in this issue here #4423

Resolution

  • Added the ability to automatically add Vitest with example tests to new projects using the svelte-create package. This should help people new to unit tests in Sveltekit get started with reasonable defaults by scaffolding the configuration for them, and giving them example tests to start out with.

Testing

  • Manually tested this with every config to make sure it worked.

Possible problems

  • I have not tested this Vitest config when emulating Sveltekit specific behavior such as $app/env and $app/stores I might need input from @benmccann on this one...

@changeset-bot
Copy link

changeset-bot bot commented Jul 25, 2022

🦋 Changeset detected

Latest commit: 01ab4ba

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
create-svelte Patch

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

Copy link
Member

@benmccann benmccann left a 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?

@dominikg
Copy link
Member

Do we want to add c8 + coverage script by default?
I think vitest asks you to install c8 if you run --coverage without it. Adding it by default means every user gets it, even if they are not interested in it.

@bertybot
Copy link
Contributor Author

Do we want to add c8 + coverage script by default?

I think vitest asks you to install c8 if you run --coverage without it. Adding it by default means every user gets it, even if they are not interested in it.

Yea, this is true. Do you think I should just remove the coverage script as well?

@dominikg
Copy link
Member

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.

@bertybot
Copy link
Contributor Author

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
Copy link
Member

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

Copy link
Contributor Author

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.

@bertybot
Copy link
Contributor Author

bertybot commented Sep 30, 2022

I pushed some updates, so this should hopefully be close to being able to be merged. The one remaining item is @dominikg's comments about global types: #5708 (review). Assuming we want to use globals, perhaps someone more familiar with TypeScript could add the types

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
@bertybot
Copy link
Contributor Author

bertybot commented Sep 30, 2022

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.

@dominikg
Copy link
Member

dominikg commented Oct 1, 2022

I don't recommend using vite.config.ts . The way vite creates a temporary vite.config.hash.js isn't great imho.

@dominikg
Copy link
Member

dominikg commented Oct 2, 2022

If we suggest colocating test files, there should be documentation about how to handle + prefixed files.
See #7121

@bertybot
Copy link
Contributor Author

bertybot commented Oct 3, 2022

I don't recommend using vite.config.ts . The way vite creates a temporary vite.config.hash.js isn't great imho.

Updated and renamed vite.config.ts to vite.config.js and tested.

@dominikg
Copy link
Member

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

@benmccann
Copy link
Member

@dominikg why would you need different TypeScript settings for unit vs app code?

@dominikg
Copy link
Member

@dominikg why would you need different TypeScript settings for unit vs app code?

eg node env vs not

@benmccann
Copy link
Member

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?

@dominikg
Copy link
Member

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.

@dominikg
Copy link
Member

dominikg commented Nov 23, 2022

is vitest-dev/vitest#2298 a blocker here? not supporting these makes some unit tests harder/ impossible

related: #6259

@bertybot
Copy link
Contributor Author

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}']
Copy link

@jorgebv jorgebv Nov 24, 2022

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.

@Rich-Harris Rich-Harris merged commit ef01a0b into sveltejs:master Nov 26, 2022
@Rich-Harris
Copy link
Member

thanks all! very happy to get this in

@bertybot
Copy link
Contributor Author

Thanks everyone!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Vitest to create-svelte
9 participants