-
Notifications
You must be signed in to change notification settings - Fork 37
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
Unit test failed when component has svelte-multiselect #48
Comments
@davipon Thanks for the detailed analysis! The error is a bit odd though. I have some local // tests/multiselect.test.ts
import { tick } from 'svelte'
import { expect, test } from 'vitest'
// all of these work for me
import MultiSelect from '../src/lib/MultiSelect.svelte'
import MultiSelect from '../src/lib' // this imports from index.ts
import MultiSelect from '../package/index' // this imports from index.js
import MultiSelect from '../package/MultiSelect.svelte'
test(`mount component`, async () => {
const options = [`Banana`, `Watermelon`, `Apple`, `Dates`, `Mango`]
const instance = new MultiSelect({
target: document.body,
props: { options },
})
expect(instance).toBeTruthy()
const lis = document.body.querySelectorAll(
`div.multiselect > ul.options > li`
)
expect(lis.length).toEqual(5)
lis.forEach((li, i) => {
expect(li.textContent).toContain(options[i])
})
}) This is my import { svelte } from '@sveltejs/vite-plugin-svelte'
export default {
plugins: [svelte({ hot: !process.env.VITEST })],
test: {
environment: `jsdom`,
},
} I'm happy to change - export { default } from './MultiSelect.svelte'
+ export { default as default } from './MultiSelect.svelte' if that's necessary. Could you do me a favor and actually go into |
I did try this but unfortunately the test still failed. export { default as default } from './MultiSelect.svelte Here are approaches I tried to find the root cause:
// import MultiSelect from 'svelte-multiselect'; // [FAILED]: import from node_modules
// import MultiSelect from 'svelte-multiselect/MultiSelect.svelte'; // [FAILED]: import from node_modules/svelte-multiselect/MultiSelect.svelte
import MultiSelect from '../svelte-multiselect'; // [PASSED]: Directly copy the package to the root of the project
test(`mount component`, async () => {
const options = [`Banana`, `Watermelon`, `Apple`, `Dates`, `Mango`];
const instance = new MultiSelect({
target: document.body,
props: { options }
});
expect(instance).toBeTruthy();
const lis = document.body.querySelectorAll(`div.multiselect > ul.options > li`);
expect(lis.length).toEqual(5);
lis.forEach((li, i) => {
expect(li.textContent).toContain(options[i]);
});
}); I've updated my reproducible repo, please kindly try: https://github.com/davipon/test-failed I was using Vitest 0.5.5 yesterday and failed the test without any informative error, but it started to yell after upgrade to 0.5.7 today: It's weird because svelte ext should already been configured in |
The problem seems to be that imports from Maybe this SO answer helps? Does it work when you exclude |
Thanks @janosh, it works after adding {
"testEnvironment": "jsdom",
"transformIgnorePatterns": ["node_modules/?!(svelte-multiselect)"],
"transform": {
"^.+\\.[t|j]s?$": "esbuild-jest",
"^.+\\.svelte$": ["svelte-jester", { "preprocess": true }]
}
}
While test still failed in Vitest, I couldn't find the corresponding config in Vitest so far, will raise a ticket there. Since SvelteKit wouldn't build code when packaging, also transformers like babel-jest, ts-jest, svelte-jester wouldn't transpile files within I think it would be great if we can add a section in README for people who will encounter the same problem, like recommended Jest config for unit testing, can also add Vitest config if there is a solution in the future. |
Thanks. Could you link this issue over there? I'd like to subscribe to your
Indeed which is why I'm very curious to hear if there's a recommended solution.
Sure that would be great! |
Based on vitest-dev/vitest#809, https://vitest.dev/config/#transformmode might be something to play around with. |
I also tried Anyway, I asked in Vitest Discord channel and got the answer!
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! |
Cool! Let's close this issue once your PR to update the readme is merged. |
…nstream testing (#54) * Add Unit Test & stackblitz link in README (closes #48) * bit more concise wording Co-authored-by: Janosh Riebesell <[email protected]>
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:
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:
I'm not familiar with creating Svelte package, but I found a difference in these two libraries'
index.js
:svelte-tiny-virtual-list:
svelte-multiselect:
Not sure if information above can help, and thanks again for your hard work!
The text was updated successfully, but these errors were encountered: