Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Unit Test Svelte Component { #if } #283

Answered by mcous
feelinglovelynow asked this question in Q&A
Discussion options

I've got simple reproduction steps that only take a minute to setup and a 50 point bounty that is provided to any viable answers on a Stack Overflow question that I asked 6 days ago with no answers yet asking how to test an { #if } logic block in a Svelte component that I would love some help with please

https://stackoverflow.com/questions/77624250/unit-test-svelte-component-if

Thank you!

You must be logged in to vote

It looks like your question isn't about how to test an {#if} block, but rather how to get the coverage report to show the {#if} branch as covered when you have tested it.

Honestly, you're going to be at the mercy of the Svelte compiler and its source maps here; getting accurate coverage reporting of transpiled languages is difficult at the best of times. There's nothing this library here does to affect it: we just mount the component using the API provided to us by Svelte.

The best advice I can give you here is: try to worry less about your coverage stats! All this testing-library stuff is more integration testing than unit testing, because you're always going through the full Svelte runt...

Replies: 1 comment

Comment options

It looks like your question isn't about how to test an {#if} block, but rather how to get the coverage report to show the {#if} branch as covered when you have tested it.

Honestly, you're going to be at the mercy of the Svelte compiler and its source maps here; getting accurate coverage reporting of transpiled languages is difficult at the best of times. There's nothing this library here does to affect it: we just mount the component using the API provided to us by Svelte.

The best advice I can give you here is: try to worry less about your coverage stats! All this testing-library stuff is more integration testing than unit testing, because you're always going through the full Svelte runtime and writing to the DOM. I find code coverage to be a little less meaningful unless you really have the ability to isolate your units. I also recommend pushing as much branching logic as possible to pure JS units that you can test in isolation; try to run as little branching logic as possible inside components.

If you get better coverage reporting with newer versions of Svelte, different coverage instrumentation, and/or different test runners, I'd be curious to hear!

You must be logged in to vote
0 replies
Answer selected by yanick
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /