-
Notifications
You must be signed in to change notification settings - Fork 258
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
Bug: Built-in <component />
functionality seems altered when shallow: true
#1277
Comments
I've typed it up based on what's in my codebase, but haven't actually run it. Hopefully, it will help explain the issue I'm running into, either way.
<template>
<component :is="someComponent" />
</template>
<script setup>
import { computed } from 'vue'
import ComponentA from './ComponentA.vue'
import ComponentB from './ComponentB.vue'
interface Props {
isB?: boolean
}
const props = defineProps<Props>()
const someComponent = computed(() => {
return isB ? ComponentB : ComponentA
})
</script>
import { mount } from '@vue/test-utils'
import { OneOrOtherComponent } from './'
describe('OneOrOtherComponent', () => {
it('renders component A by default', () => {
const wrapper = mount(OneOrOtherComponent, { shallow: true })
// This outputs "<some-component />"
console.log(wrapper.html())
// This fails
expect(wrapper.findComponent({ name: 'component-a' }).exists()).toBe(true)
})
}) |
Thanks for the great report - I will try get some 👀 on this soon. |
I know the name resolution a bit, so I took a look at this. VTU searchs the component within the exposed WorkaroundsCurrently I have no solution for this but some workarounds:
expect(wrapper.findComponent(ComponentA).exists()).toBe(true)
<template>
<component :is="isB ? ComponentB : ComponentA" />
</template> |
@freakzlike Thanks for the ideas!
I'll give this one a try and report back.
Do you mean, in a stub component passed to
This one would be a bit ugly to evaluate in the template, since the actual implementation I'm working on chooses between about 5 different components based on a key. I'll see what I can do that could maybe trick the VTU. |
On 2 I mean this:
<template>
<!-- ... -->
</template>
<script>
export default {
name: 'ComponentA'
}
</script>
<template>
<!-- ... -->
</template>
<script>
export default {
name: 'ComponentB'
}
</script> |
Workaround 1This seems to work in most cases. However, there are a few caveats remaining:
I think the first two points make sense under the circumstances, but the third point was a bit of a surprise. Workaround 2This did not seem to have an effect. I could try poking around at it more. Workaround 3This works, but only when the computed is also removed from the component. It is not enough for the reference to be removed from the template. From poking at it a bit, it seems that the name of the first computed that returns the same component that is in
This seems to be the VTU behavior @freakzlike mentioned. |
Well named slots are never rendered with the auto stub from VTU: What I tried to explain is exactly this logic: test-utils/src/utils/componentName.ts Lines 9 to 15 in d414157
And the arguments at this scenario type = ComponentA
setupState = {
someComponent: ComponentA,
ComponentA: ComponentA,
ComponentB: ComponentB
} So Update for Workaround 2I noticed, that the name of the component is used as fallback after the |
Regarding named slots, the important note is the third point, that even with component stub objects registered in Regarding |
@phobetron This issue is a bit long and I'm getting lost in the details. |
@cexbrayat I'll see what I can do over the weekend 👍🏻 |
@cexbrayat I've added minimal tests in a PR: #1312 I'm not sure whether the tests are appropriately arranged, but let me know and I can make changes. |
Describe the bug
When shallow-mounting a component that contains the built-in
<component />
component, its behavior seems to be altered.To Reproduce
In my current case, I'm testing an SFC with
script setup
that has<component :is="someComponent" />
in its template.someComponent
is a computed property that returns a value of the typeComponent
.When testing with
shallow: true
, a component named<some-component />
is rendered. It seems to match the name of the computed property.Expected behavior
When testing with
shallow: true
, a component named<custom-component />
is rendered. It should match the return value of the computed property.Related information:
@vue/test-utils
version: 2.0.0-rc.18Vue
version: 3.2.26node
version: 16.13.0npm
(oryarn
) version:yarn
1.22.17Additional context
When using
shallow: false
, or running normally in the browser, the built-in<component />
component functions as expected, and<custom-component />
is rendered.I believe it is not possible to stub the functionality of
<component />
as it is a built-in component.The components that are rendered using
<component />
are fairly complex, and are tested separately, so we are hoping to merely inspect the values passed to those components instead of reaching through their hierarchies and inspecting their behaviors.The text was updated successfully, but these errors were encountered: