Author -  Sai gowtham

Testing Props in Vue components using Jest

In this tutorial, we are going to learn about how to test props in vue components using jest and vue-test-utils.

Props in vue components help us to pass the data from parent components to child components.

The example component we are testing.

Post.vue
<template>
  <h1>{{title}}</h1></template>

<script>
export default {
  props:['title']};
</script>

In the above component, we have declared a prop called title.

When we unit test a component we need to provide a correct prop to the component at the time of mounting. so that we can test does the component is receiving correct props are not.

In vue-test-utils shallowMount method takes an options object as a second argument by using this object we can pass props to the component.

Let’s write a test for the Post.vue component.

Post.test.js
import Post from '../src/components/Post.vue'
import { shallowMount } from '@vue/test-utils';
describe('Testing Component props', () => {
    const wrapper = shallowMount(Post, {
        propsData: {
            title: "First post" //passing prop to component        }
    });

    it('checks the prop title  ', () => {
        expect(wrapper.props().title).toBe('First post');    })
})

In the above code, we first passed a prop to the component inside shallowMount method and it returns a wrapper object which contains a props() method.

The props() method returns an object containing props of the component currently have, like in the above code we are asserting the prop title has a value First post.

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 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY