diff --git a/src/vnodeTransformers/stubComponentsTransformer.ts b/src/vnodeTransformers/stubComponentsTransformer.ts index 7f1a110fd..2a9788b85 100644 --- a/src/vnodeTransformers/stubComponentsTransformer.ts +++ b/src/vnodeTransformers/stubComponentsTransformer.ts @@ -117,8 +117,13 @@ export const createStub = ({ // Also having function text as attribute is useless and annoying so // we replace it with "[Function]"" const stubProps = normalizeStubProps(props) - - return h(tag, stubProps, renderStubDefaultSlot ? slots : undefined) + // if renderStubDefaultSlot is true, we render the default slot + if (renderStubDefaultSlot && slots.default) { + // we explicitly call the default slot with an empty object + // so scope slots destructuring works + return h(tag, stubProps, slots.default({})) + } + return h(tag, stubProps) } } }) diff --git a/tests/mountingOptions/global.components.spec.ts b/tests/mountingOptions/global.components.spec.ts index f60225c1f..46c8c3d38 100644 --- a/tests/mountingOptions/global.components.spec.ts +++ b/tests/mountingOptions/global.components.spec.ts @@ -72,6 +72,7 @@ describe('global.components', () => { spy.mockRestore() expect(wrapper.text()).toBe('Global') }) + it('render children with shallow and renderStubDefaultSlot', () => { const Child = defineComponent({ template: '

child

' @@ -97,4 +98,33 @@ describe('global.components', () => { '' ) }) + + // https://github.com/vuejs/test-utils/issues/2395 + it('render children with shallow and renderStubDefaultSlot with v-slot', () => { + const Child = defineComponent({ + template: '

child

' + }) + const Component = defineComponent({ + template: + '
hello{{ count }}there
', + components: { + Child + } + }) + const wrapper = mount(Component, { + shallow: true, + global: { + renderStubDefaultSlot: true + } + }) + + // count is undefined, but doesn't throw an error + expect(wrapper.html()).toEqual( + '
\n' + + ' \n' + + '
hellothere
\n' + + '
\n' + + '
' + ) + }) })