question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit test failed when component has svelte-multiselect

See original GitHub issue

Hi @janosh, I like to thank you for creating such a great component! I love it!

Everything works great so far (v4.0.0) in my svelte app until I tried to add some unit tests. Test failed when I tested component which has svelte-multiselect, Jest threw error (Vitest broke right away) like this: Screen Shot 2022-02-26 at 12 45 38 PM

I’d already setup transformers so it’s not a ESM problem in Jest, and other tests passed with different svelte libraries.

Here is a reproducible repo that you can check: https://github.com/davipon/test-failed I simply import svelte-tiny-virtual-list & svelte-multiselect in the same test file and try to render them. Test would fail before rendering Multiselect:

test('render multiselect', () => {
  render(Multiselect, { options: webFrameworks });
});

I’m not familiar with creating Svelte package, but I found a difference in these two libraries’ index.js: svelte-tiny-virtual-list: Screen Shot 2022-02-26 at 1 02 41 PM

svelte-multiselect: Screen Shot 2022-02-26 at 1 04 51 PM

Not sure if information above can help, and thanks again for your hard work!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
daviponcommented, Feb 28, 2022

I also tried transformmode but it failed as well.

Anyway, I asked in Vitest Discord channel and got the answer! We need to use dep.inline in Vitest:

test: {
  globals: true,
  environment: 'happy-dom',
  deps: {
    inline: [/svelte-multiselect/]
  }
}

Now we know how to deal with transpiling errors when unit testing most of the Svelte component packages (I believe) in both Jest and Vitest, I’ll create a PR to add a Unit Test section in README with examples.

Thanks again @janosh, your comments really helped me to dig in and find the root cause, please kindly close this issue, have a good one!

1reaction
janoshcommented, Feb 27, 2022

While test still failed in Vitest, I couldn’t find the corresponding config in Vitest so far, will raise a ticket there.

Thanks. Could you link this issue over there? I’d like to subscribe to your vitest issue.

this seems to be a general problem for future Svelte ecosystem when it comes to unit testing.

Indeed which is why I’m very curious to hear if there’s a recommended solution.

I can create a PR for that if you think it’s OK.

Sure that would be great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected token when using @testing-library/svelte #140
The issue is that your code has not been transpiled before testing and Jest doesn't know what to do with the export keyword....
Read more >
My jest unit test case is failing for Material UI multi select
This test case is failing. Getting error like - TypeError: Cannot read property 'selectedOptions' of undefined . Also code is not covered for ......
Read more >
Svelte Component Test Recipes - David Peng
In this blog post, we'll use vitest , @testing-library/svelte , and svelte-htm to test Svelte components that seemed to be hard to test....
Read more >
MultiSelect • REPL • Svelte
1 · script> ; 2. import MultiSelect from './MultiSelect.svelte'; ; 3. let value; ; 4 · script> ; 5.
Read more >
Mocking Svelte components - DEV Community ‍ ‍
Writing unit tests for Svelte (8 Part Series) ... In this post I'll explore mocking, which as a topic has attracted a lot...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found