question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

named slots not rendering in children of other components with shallowMount

See original GitHub issue

Version

1.0.0-beta.31

Reproduction link

https://github.com/vetruvet/vue-test-utils-child-with-slots-bug

Steps to reproduce

Install dependencies then npm test.

What is expected?

Expect all test cases in the spec to pass.

More specifically, I would expect components with named slots to render their slots regardless of where they are in the component hierarchy.

What is actually happening?

shallowMount test with a component with named slots inside of another component fails because it does not render its slots.

In cases where the component with named slots is not inside another component, the slot rendering works as expected.


In beta.30, none of the shallowMount tests using the component with two slots pass. I think this may have something to do with supporting the new v-slot syntax which was added in beta.31.

Potentially related to #1307, but it’s not the same issue because that one does not relate to the new v-slot changes in beta.31. However, I think #1309 may address this.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

4reactions
danieltiancommented, Apr 21, 2020

To elaborate, when using shallowMount, the default slot is rendered, but named slots are not. I would not be OK with not rendering any slots in stubbed child components, because then it will be impossible to test that the correct template is sent to the child. We can currently test for attributes and props using childWrapper.attributes() and childWrapper.props(), but nothing equivalent exists for templates. Using mount is not an option because that turns the unit test into an integration test, which means that all descendant children need to be set up properly in order for the component to mount.

As a workaround for now, explicitly setting the stubs: will render the named slots:

const wrapper = shallowMount(RootComponent, {
  stubs: { ChildComponent }
}

expect(wrapper.find(ChildComponent).text()).toContain('some text');
2reactions
CyberAPcommented, Sep 8, 2021

Here’s a workaround without using mount or de-stubbing with actual component:

function mockSlots(scopedSlots) {
  return {
    render(h) {
      return h('div', scopedSlots.map(([name, slotProps]) => this.$scopedSlots[name](slotProps)));
    },
  };
}
<!-- Foo.vue -->
<Bar>
  <template #namedSlot>
    <Baz />
  </template>
</Bar>
it('renders named slot', () => {
  const wrapper = shallowMount(Foo, { stubs: { Bar: mockSlots([['namedSlot', null]]) } });
  expect(wrapper.findComponent(Baz).exists()).toBe(true) // true
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Stubs and Shallow Mount | Vue Test Utils
Since shallow stubs out all the content of a components, any <slot> won't get rendered when using shallow . While this is not...
Read more >
New: Guideline for component unit testing with `shallowMount ...
This doesn't apply if you stub children to render named slots. Unfortunately, this case is not covered with VTU and we need to...
Read more >
Vue.js Slots: <template> tags used as slot content
When you call shallowMount , the child components are stubbed with ... the layout component will not render the named slots and shallowMount...
Read more >
A Guide to Vue Slots
Slots are another way in Vue for a component to inject content into a child component. This does this using template code.
Read more >
Testing Vue.js Components with Jest - Section 1
Learn shallow rendering, snapshot testing and dependency mocking, among other techniques. ... Write the first Vue.js Component Unit Test in Jest.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found