Finding an Element by Class in a Vue Unit Test

Devanshu Agarwal

Written by Devanshu Agarwal /

In this blog post, we'll look at how to find an element by its class name in a Vue unit test.

Initial Setup

We'll use the jest library for writing the tests and the vue-test-utils library for interacting with the Vue component.

Next, let's import the mount method from the vue-test-utils library and the component we want to test:

import { mount } from 'vue-test-utils';
import MyComponent from './MyComponent.vue';

Writing the Test

Now that our test environment is set up, let's write a test that finds an element by its class name:

describe('MyComponent', () => {
  it('should find an element by class name', () => {
    const wrapper = mount(MyComponent);

    const element = wrapper.find('.my-class-name');


In this example, we use the mount method to render the MyComponent component, and the find method on the wrapper object to find the element with the class my-class-name.

Finally, we use the expect method from the jest library to assert that the element exists.