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.

Mounting only the slots of stubbed child components

See original GitHub issue

Original: https://github.com/vuejs/vue-test-utils/issues/1216


What problem does this feature solve?

We currently have a helper file in our codebase we pull in to testing files, it contains this beauty:

/**
 * Create a component stub that will render all included slots from the
 * parent component. This lets you test the slots you've included in child component
 * without having to fully mount the child component.
 *
 * * Notes on implementation *
 * There is no one place this is clearly laid out. This thread gave the starting point:
 *
 * https://github.com/vuejs/vue-test-utils/issues/85
 *
 * but modifying the function requires understanding Vue's functional components
 * and the `render` function:
 *
 * https://vuejs.org/v2/guide/render-function.html
 *
 * especially the arguments that `createElement` can take:
 *
 * https://vuejs.org/v2/guide/render-function.html#createElement-Arguments
 *
 * In short, this produces a <div> with only the child components slots, but none of
 * its other functionality.
 *
 * @return {object} The functional component definition
 */
componentStubWithSlots: function () {
  return {
    render: function (createElement) {
      return createElement('div', [Object.values(this.$slots)]);
    }
  };
}

It would be nice if there was a more elegant way of handling this, or at least better documented (not requiring links to 3 different places).

What does the proposed API look like?

That helper function gets used like so:

test('Show only mine checkbox adds showOnlyMine to the query params', async () => {
  const wrapper = shallow(reservationsList, {
    store,
    localVue,
    stubs: {
      RouterLink: RouterLinkStub,
      'base-table': helpers.componentStubWithSlots()
    },
    mocks: { $ga }
  });

  let checkbox = wrapper.find('[data-test="showOnlyMineCheckbox"]');
  checkbox.trigger('click');

  expect(wrapper.vm.paramsForGettingListItems)
    .toEqual({ showOnlyMine: true });
});

I think it would be nicer if there was something like that built in, so we could just do this:

test('Show only mine checkbox adds showOnlyMine to the query params', async () => {
  const wrapper = shallow(reservationsList, {
    store,
    localVue,
    stubs: {
      RouterLink: RouterLinkStub
    },
    stubComponentSlots: [
      'base-table'
    ],
    mocks: { $ga }
  });

  let checkbox = wrapper.find('[data-test="showOnlyMineCheckbox"]');
  checkbox.trigger('click');

  expect(wrapper.vm.paramsForGettingListItems)
    .toEqual({ showOnlyMine: true });
});

We use this helper a lot, it seems like the kind of common “utility” that would be in Vue-Test-Utils.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
TheJaredWilcurtcommented, Apr 21, 2020

Another use case for this to shallow mount a view/component that contains a 3rd-party component. Where you don’t want to load the complexity of that 3rd party component, but you do want to test your code being passed into the slot. It can make snapshots more useful and less cluttered.

1reaction
lmiller1990commented, Apr 21, 2020

@dobromir-hristov is looking into this. The idea is looking to be to provide this as a config users can enable, so you can have the behavior you like. eg config.shallowMount = { renderSlots: true } or something, since the default is stubbing them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mounting only the slots of stubbed child components #69
Create a component stub that will render all included slots from the * parent component. This lets you test the slots you've included...
Read more >
Stubs and Shallow Mount | Vue Test Utils
VTU has a shallow mounting option that will automatically stub out all the child components: test('shallow stubs out all child components', ...
Read more >
Vue how to test component with slot and slot-props
A toBe matcher would check that all the text in a component strictly equals the expected value.
Read more >
Migration to Vue 3 - GitLab Docs
To fix rendering slots in shallowMount , we need to stub a child component with slots explicitly. <!-- MyAwesomeComponent.vue --> <script> import ...
Read more >
[vue-test-utils]: operations on destroyed component are ...
A stubbed child component is a replacement for a child component rendered by the component under test. Imagine you have a ParentComponent component...
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found