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.

`<anonymous-stub>` when running in Vitest + Vue 3 + Vue test utils `{ shallow: true }`

See original GitHub issue

Issue

When using single/multiple stubs option with vue test utils it renders the whole inner html of the component, like so:

import { describe, it, expect } from 'vitest';
import { mount } from '@vue/test-utils';

import SomeComponent from './SomeComponent.vue';

test('stubs component', () => {
  const wrapper = mount(SomeComponent, {
    global: {
      stubs: {
        SomeComponent: true
      }
    }
  })

  console.log(wrapper.html())
  **Expected**
  /*
    <some-component-header-stub></some-component-header-stub>
    <h1>Welcome to Vue.js 3</h1>
  */

  **Actual**
  /*
    <div class="text-2xl font-bold">This header should be stubbed</div>
    <h1>Welcome to Vue.js 3</h1>
  */

  expect(wrapper.html()).toContain('Welcome to Vue.js 3')
})

When using with shallow mount the children component names become anonymous-stub:

test('shallow stubs out all child components', () => {
  const wrapper = mount(ComplexComponent, {
    shallow: true
  })

  console.log(wrapper.html())
  **Expected**
  /*
    <h1>Welcome to Vue.js 3</h1>
    <complex-a-stub></complex-a-stub>
    <complex-b-stub></complex-b-stub>
    <complex-c-stub></complex-c-stub>
  */

  **Actual**
  /*
    <h1>Welcome to Vue.js 3</h1>
    <anonymous-stub></anonymous-stub>
    <anonymous-stub></anonymous-stub>
    <anonymous-stub></anonymous-stub>
  */
})

both cases are wrong, is there a way to make this work?

Use Case

When using tailwind you can catch style changes with snapshot tests. which I assume are much faster, than cypress component tests.

And you only want to see the styles of the current component under test, so as not to make tests fail when the child element styles update. to avoid flaky tests.

as a workaround you can import components directly, without using unplugin-vue-components and it works, but that obviously defeats the purpose of this plugin.

Thank you

This plugin saves a ton of time and boilerplate would be awesome to get this little bit to work 😃

I’d give it a show with a PR if you could just point me in the direction

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:9

github_iconTop GitHub Comments

2reactions
TadasMilcommented, Jul 17, 2022

I’m facing the same issue here

1reaction
fvanwijkcommented, Aug 9, 2022

When unplugin-vue-components is used, it is impossible to us the stubs feature of Vue Test Utils. This makes unplugin-vue-components practically unusable with Vitest.

Here is an example repo created with npm create vite@latest (vue-ts) + @testing-library/vue + vitest + unplugin-vue-components.

(It also fails with Vue Test Utils instead of Testing library)

I’m not certain this is related to the first comment but at least there seems to be anything wrong with stubbing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stubs and Shallow Mount | Vue Test Utils
Stubs and Shallow Mount #. Vue Test Utils provides some advanced features for stubbing components and directives. A stub is where you replace...
Read more >
Stubbing components - Vue Testing Handbook
When writing unit tests, often we want to stub parts of the code we are not interested in. ... import { shallowMount, mount...
Read more >
When to "Unstub" a Component in a Vue.js Unit Test
Vue Test Utils can automatically do this for you with a feature called shallowMount . But what happens if a component is tightly...
Read more >
Using Vue Test Utils in Vitest - YouTube
Using Vue Test Utils in Vitest · Testing Vue components is a crucial step to ensuring your frontend app is working as expected....
Read more >
mount() and shallowMount() from @vue/test-utils throw ...
I am trying to set up unittesting for my Vue app but everytime I run my test the mount or shallowMount function throws...
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