Writing Testing Library tests that use certain Skeleton components #2891
-
I'm working on a project that uses SvelteKit, Skeleton, and Testing Library. What is the best way of using and testing Skeleton components like Popup or Modal in Testing Library tests? Can they be used directly, or do they all need to be mocked? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
Hey @is-sematell, so two things here:
Skeleton tests our own components and features, so you do not need to. That would be redundant. Here's our modals test for example. What you should focus on is testing your own custom modals, which utilize a component embedded within a modal window. In that case, you would test the component (directly in isolation) just like any other, and Outside the scope of the wrapping modal system (which again is already tested on our side). For overlay elements (popovers, modals, etc), those can be a bit harder to test with unit tests (ex: Vitest + Testing Library) since you need a trigger, you need the test to click the trigger, await the modal to display, then test the results. But that is a scenario Testing Library can fulfill. Typically paired with an async task to await the change in interface. You can see a brief example of triggering clicks here: One technique I've used with success here is to create a small co-located component alongside the test that is purpose built to replicate a trigger + modal/popover in isolation. That's the primary goal of unit tests after all - testing as isolated as possible. That's a bit closer to integration testing, due to the extra scaffolding, but still something these tools are equipped to handle. Finally, if you really need to test a feature as it functions in the user flow. That's where e2e testing may be a lot more valuable. You can use testing tools like Playwright for that. As for the specifics for implementing the tests themselves, that's a bit outside the scope of our support. But I would recommend reading through the Testing Library documentation to understand how it operates. However, Skeleton is open source so you can reference our component tests here:
Note that our v3 tests a bit better quality, as we've matured in our understanding of the testing tooling, but currently limited only to our React components. This is due to Vitest and Test Library not having thorough documentation for the Svelte 5 pre-release versions. Now that Svelte 5 has launched, we expect that to change, and of course bring parity to our new components. Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
For anyone who is also running into this issue, I've opened a ticket in the testing-library repo They have suggested two workarounds, and both of them work:
|
Beta Was this translation helpful? Give feedback.
Skeleton has no special requirements that would require you to use any configuration beyond what Test Library itself requires. If you follow my recommendation above, typically you're just testing components, which is what Vitest with Testing Library is all about.
I'll emphasize again, I can help push you in the right direction (as far as recommendations), but it's outside the scope of our support to teach you how to use Testing Library. I strongly recommend you read through their documentation, look at guides, and understand the plethora of options they provide.
https://testin…