by Sai gowtham

Testing Vue.js classes and styles using jest

In this tutorial, we are going to learn about how to test the classes and styles in vue by using jest and vue-test-utils.

If you don’t know how to configure jest with vue, you can check out my testing introduction tutorial.

The example component we are testing.

Post.vue
<template>
  <div class="post" style="color:black">    <h1>This is post title</h1>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
  </div>
</template>
<script>
export default {};
</script>

In this component, we have a div element with post class and style attribute.

Let’s write tests for this component.

Testing component classes

Vue-test-utils provides us a classes() method, it returns the array of class names of the root element.

Post.test.js
import { shallowMount } from '@vue/test-utils';
import Post from '../src/components/Post.vue'
describe('Testing classes and style', () => {
    const wrapper = shallowMount(Post);
    it('checks the class to be post', () => {
        expect(wrapper.classes()).toContain('post');    })
})

In the above test, we are checking for the root element has a class name post.

Testing component style

We can directly access the style property inside the wrapper.element object.

Post.test.js
import Post from '../src/components/Post.vue'
import { shallowMount } from '@vue/test-utils';
describe('Testing classes and style', () => {
    const wrapper = shallowMount(Post);
    it('checks the class ', () => {
        expect(wrapper.classes()).toContain('post');
    })

    it('checks the inline style color:black', () => {
        expect(wrapper.element.style.color).toBe('black')    })
})

Note: By default wrapper object contains the root element class names and style, If you want to test the particular element classes in your component you need to use find( ) method.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2021 (Beginner + Advanced)
JavaScript - The Complete Guide 2021 (Beginner + Advanced)
64,026 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
152,857 students enrolled
48.5 hours of video content
View Course