Skip to content

Can not get lazy-loaded / async component #1944

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

Closed
ninofiliu opened this issue Dec 31, 2021 · 2 comments
Closed

Can not get lazy-loaded / async component #1944

ninofiliu opened this issue Dec 31, 2021 · 2 comments

Comments

@ninofiliu
Copy link

ninofiliu commented Dec 31, 2021

Subject of the issue

.getComponent fails to get a component when it is lazy loaded

Steps to reproduce

Let A and B be two components

A.vue

<template>
  <div><B/></div>
</template>

<script>
import B from './B.vue';

export default {
  components: {
    B,
  },
};
</script>

B.vue

<template>
  <div>Hello from B</div>
</template>

This rightfully works

import { shallowMount } from '@vue/test-utils';
import A from './A.vue';
import B from './B.vue';

const wrapper = shallowMount(A);
wrapper.getComponent(B);

but when changing A to import B asynchronously, it does not anymore

A.vue

<template>
  <div><B/></div>
</template>

<script>
export default {
  components: {
    B: () => import('./B.vue'),
  },
};
</script>

I made a reproducible repo
https://github.com/ninofiliu/vue-test-bug

Expected behaviour

Gets the component

Actual behaviour

Fails with

Unable to get [object Object] within: <div>
  <!---->
</div>

Possible Solution

  • Adding a await wrapper.vm.$nextTick() does not help
  • Stubbing B fixes it, but I don't think it should be necessary
    const wrapper = shallowMount(A, { stubs: { B }});
@ninofiliu ninofiliu changed the title Can not get async component Can not get lazy-loaded / async component Dec 31, 2021
@ninofiliu
Copy link
Author

ninofiliu commented Dec 31, 2021

Used the same solution as in #1279 and it works as expected

https://github.com/ninofiliu/vue-test-bug/commit/b852816f575da8734e0c20b24046c656c41d11da

babel.config.json

{
  "presets": [
    ["@babel/preset-env", { "targets": { "node": "current" } }]
  ],
  "plugins": ["dynamic-import-node"]
}

Thanks @hachichaud for pointing me in this direction!

I do think this workaround belongs in the documentation, though

@ebisbe
Copy link
Collaborator

ebisbe commented Jan 23, 2023

@lmiller1990 Should we update the docs with any information?

@ebisbe ebisbe closed this as completed Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants