-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
vue: dynamic component imports are not resolved #1328
Comments
If I add this: return new Promise((resolve) => {
setInterval(() => {
console.log(wrapper.html());
if (wrapper.text().includes('1 x 2')) {
resolve();
}
});
}); Promise is resolved on the second tick 🤔 |
I experienced the same issue. Asked a question 2 days ago here: #1322 There you can find a link to a codesandbox example. |
I am not sure this is how it should work in Vitest. In Jest this worked because the code was essentially: const Hello = defineAsyncComponent(() => Promise.resolve(require('./path'))) Meaning, since it's synchronous we can just queue a microtask and wait for it to end, but in Vitest this is transformed to this: const Hello = defineAsyncComponent(() => __vite_import__('./path'))
// __vite_import__ is asynchronous And so, since I do not know, if it can be fixed on Vitest side (maybe, we can provide utility like |
I found a way to load it, using Vitest internals: await flushPromises() // this is needed for vue to start importing
const worker = globalThis.__vitest_worker__
const promises = [...worker.moduleCache.values()]
.filter(m => !m.exports && m.promise)
.map(m => m.promise)
await Promise.all(promises) // this is needed for us to wait until module is loaded
await flushPromises() // this is needed for vue to rerender component To break it down,
I found a bug that Vitest stores some modules twice, but it is out of scope of this issue. I am not sure what else I can do to help you, @cexbrayat |
@sheremet-va Thanks for the workaround, that works 👍 Do you think this internal machinery could be exposed on a utility function by Vitest (like |
I think we can expose waiting for modules to load, but I don't see how flushes fit there, so you would need to also make a wrapper in test-utils. |
Yeah, I think we can either offer a wrapper in VTU, or at least document how to build it once a utility is available 👍 |
Now that |
@cexbrayat Should |
@binarious I actually don't know sorry: I'm part of the Vue team but not of the Svelte one, so I don't know what Svelte does in that case. You should probably open a dedicated issue 👍 |
I'm having troubles when trying to load nested async components (first async components loads another async component). When using just one, it works fine. See the example. Any idea what am I doing wrong or the |
OK, looks like the "solution" is to call the |
Yes, I don't think it works for nested imports. When it's called it looks if there are any modules that are not loaded and waits until it's loaded. But it doesn't check if there are any not loaded modules after the first call. We should probably do this, PR welcome. |
I'm afraid this is above my skill set. All I can do is to create a new issue or update the docs that it needs to be called multiple times when handling multiple imports. |
Hey, sadly for me the issue still exists. In our vue component we are importing another component from a file which is importing and exporting all existing components within this component-lib. I still have to call dynamicImportSettled multiple times and even then its flaky. When I call it like 5+ times then it starts working all the time. What I found out so far is that this utility is not even recognising that this is a dynamic export that it should wait for. This is using:
|
Describe the bug
With Vue, you can define async components with a dynamic import like the following:
Hello.vue
is then loaded only when the user clicks on the buttonShow Hello
But a unit test checking this behavior fails with Vitest:
Maybe I'm missing something obvious in the test setup, but it looks like the promise is never resolved.
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-bmfn8b
System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: