Vite/Vitest React Kitchen Sink Demo #459
Replies: 2 comments 2 replies
-
We don't have global.jest = global.vi
Fetch is not implemented in JSDOM, but happy-dom provides pollyfil with
It has a
Yeah, coverage is not great at the moment. We have a lot of issues with it right now.
Thank you for your kind words! |
Beta Was this translation helpful? Give feedback.
-
I thought I'd open a PR for Storybook testing but sadly Storybook does not support |
Beta Was this translation helpful? Give feedback.
-
Greetings!
I have created a fully-featured React demo that shows off Vite and Vitest using an app based on one I use for training at my company (the app is a simple vehicle manager.)
I have spent some time converting this from Create React App to Vite and Vitest and thought I'd share the code as it highlights some quirks with Vitest in particular that I thought warranted a discussion. Plus I like to share and maybe others will find it useful.
You can find the source of the app here: https://github.com/robcaldecott/vite-kitchen-sink
The app showcases a lot of modern React goodies:
react-query
for API handlingreact-intl
and corresponding babel plugin for automatic message IDsfetch
wrapperBut the relevant part is that it uses Storybook as a basis for the component unit tests via a combination of
@storybook/testing-react
and React Testing Library. At my company this is a very popular pattern with devs: Storybook is the core dev tool and reusing stories as your test units makes things easier to reason about.vitest quirks and issues
whatwg-fetch
My first issue was
fetch
not working when running tests so I copied what CRA does and addedwhatwg-fetch
to the project which I load via a test setup file. However. I was then greeted with the following errors:I'm not 100% sure what's going on here as the
package.json
for this dependency has amain
and amodule
but it looks like themain
entry points to a UMD file so maybe that's the reason? I fixed it by addingwhatwg-fetch
todeps.inline
but it was confusing. Not necessarily a criticism of Vitest but curious.jest-fn
Next up was
jest.fn
is missing which I naively thought would be covered by Vitest. I haveglobals: true
in my config but I guess that's only forexpext
,it
,test
, etc.? I had toimport { fn } from "vitest"
and use that instead. Not a big deal but makes converting a large set of tests a bit more effort.Coverage issues
The coverage report is producing some bizarre output and I think this is an actual bug. If I run all my tests I see some reports of code not being covered when it definitely is and the corresponding HTML report makes no sense. Here's one example:
That's a prop that's passed to a child component. I don't see how that code isn't executed.
Here's another weird one:
Definitely not right!
Excluding files
I also had to mess with the coverage exclude settings else some of the tests themselves ended up included in the report! Not all of them either which was odd.
I settled on this in the end:
Again, nothing that couldn't be fixed with some config but might be worth me raising an issue.
The takeaway is that imho the Vitest team have done an AMAZING job with this project. While there are some things to iron out it's so much more stable than I expected and configuration is so simple compared to
jest
.I plan to add a simpler example of using Storybook for your tests to the Vitest examples in the next few days.
Beta Was this translation helpful? Give feedback.
All reactions