- Lab
- Core Tech

Guided: Testing a Budgeting Application in Vue 3
This Guided Code Lab is designed to help you practice and develop your understanding of core Vue testing concepts. This includes: rendering components in test files, finding elements from the DOM, and reviewing component properties. A series of guided steps will give you a walkthrough of how to apply your knowledge of these concepts to build tests for an existing Vue component that is found in the larger application. By the end of the lab, you will have written tests for an existing Vue component.

Path Info
Table of Contents
-
Challenge
Introduction
In this lab, you will be writing tests for an existing simple budgeting application using the Vue front-end framework. You will be writing tests for the BudgetForm component. The test file you will be working on can be ran using the
npm run test:unit run BudgetForm
command. This way you will have the freedom to run the tests.The app can be run at anytime by clicking the Run button or by running the command
npm run dev -- --host
within the Terminal. You can view the app'ss webpage in the Web Browser tab using the Refresh icon button.By the end of this lab, you will have a deeper understanding of how to test a Vue component and the objects used when doing so.
-
Challenge
Testing the BudgetForm Component's Initial Data
Throughout this lab, you will be testing the
BudgetForm
component. This component contains the form and input elements that allow the user to specify a total budget amount and the cost of each item.Each instance of a component tracks its own state, or internal data, aptly stored within the
data()
function. This function returns an object containing the reactive state data.In this step, you will test that the internal data state is initialized correctly. You will implement the
correctly sets initial data
spec found insrc/components/__tests__/BudgetForm.spec.js
.If you would like to preview the solution, it can be found in the
soln/SolnBudgetComponentForm.spec.js
file. As you learn about Vue Testing, you'll review certain objects and methods provided to you.In the upcoming tasks, use
shallowMount
with the component that you are testing as the argument.shallowMount
returns aWrapper
object. You will often save this object as a variable titled,wrapper
. This way you can use this instance variable to query the rendered component.The variable
wrapper
that you create contains the mounted and rendered Vue component that was passed toshallowMount
.The variable
wrapper
is of typeWrapper
as defined inVue Test Utils
documentation.For this step, it is important to note that the
Wrapper
object has the propertyvm
of typeComponent
. You can access instance methods and properties of ourBudgetForm
component withwrapper.vm
. Within thedata()
function ofBudgetForm.vue
, an object is created that contains an empty description, an item amount of 0, and a budget amount of 0.You will test that when the component is mounted, these component properties are initialized correctly.
Like many JavaScript testing libraries, Vitest incorporates methods such as
describe
,it
, andexpect
. You can leverage a Jest like API to confirm that you are getting expected values using methods liketoBe
,toEqual
,toBeTruthy
,toHaveBeenCalled
, andtoContain
. You'll be making use of these methods in the forthcoming tasks and steps to finalize the tests.For an in-depth understanding of Vitest and the methods it offers, refer to Vitest Expect.
-
Challenge
Testing the BudgetForm Component's submitBudgetAmountForm Instance Method
The
methods
property of a component details all of the functions it is able to perform. TheBudgetForm
component hassubmitBudgetAmountForm()
andsubmitItemForm()
methods which are responsible for emitting the user input data to be displayed.In this step, you will test that calling
BudgetForm
’ssubmitBudgetAmountForm
emits theset-budget-amount
event with the correctbudgetAmount
value.You will implement the
emits "set-budget-amount" event with correct payload when submitBudgetAmountForm method is called
spec found insrc/components/__tests__/BudgetForm.spec.js
.If you would like to preview the solution, it can be found in the
soln/SolnBudgetComponentForm.spec.js
file. -
Challenge
Testing the BudgetForm Component's submitItemForm Instance Method
The
methods
property of a component details all of the functions it is able to perform. For theBudgetForm
component, it hassubmitBudgetAmountForm()
andsubmitItemForm()
methods which are responsible for emitting the user input data to be displayed.In this step, you will test that
BudgetForm
’ssubmitItemForm
emits theadd-item
event with the correctdescription
anditemAmount
values.You will implement the
emits "add-item" event with correct payload when submitItemForm method is called
spec found insrc/components/__tests__/BudgetForm.spec.js
.If you would like to preview the solution, it can be found in the
soln/SolnBudgetComponentForm.spec.js
file. -
Challenge
Testing BudgetForm Rendered HTML for Submitting Overall Budget Amount
The last part in the
BudgetForm
component to test is the HTML template. You will use the following steps to test that each HTML element:- Exists in the DOM
- Contains the appropriate attributes
- Calls the appropriate methods when certain events are invoked
In this step, you will test the form used to update the overall Budget Amount.
You will implement the
contains submitBudgetAmountForm element
spec found insrc/components/__tests__/BudgetForm.spec.js
.If you would like to preview the solution, it can be found in the
soln/SolnBudgetComponentForm.spec.js
file. -
Challenge
Testing BudgetForm Rendered HTML for Submitting Budget Item
The last part in the
BudgetForm
component to test is the HTML template. You will use the following steps to test that each element:- Exists in the DOM
- Contains the appropriate attributes
- Calls the appropriate methods when certain events are invoked
In this step, you will test the form used to update the created budget items that deduct from the overall budget.
You will implement the
contains submitBudgetItemForm element
spec found insrc/components/__tests__/BudgetForm.spec.js
.If you would like to preview the solution, it can be found in the
soln/SolnBudgetComponentForm.spec.js
file. -
Challenge
Next Steps
Now that you have experience writing tests for an existing Vue component, you are welcome to look at any other tests under the
src/__tests__/
directory to test other components found in this Budgeting Application. In these tests, you will see examples of the following:- Passing props when creating the wrapper component.
- Using
wrapper.vm.$options.props
to predict what props should be expected and defined. - How
options
on aComponent
object can be used to access any of theoptions
that are used to define the component. - Using
wrapper.text()
to return the text content within the wrapper to make sure certain text appears. - Using
wrapper.findAllComponents({Some Component})
to return aWrapperArray
of all matching Vue Components. - Using
mount({Some Component})
instead ofshallowMount({Some Component})
. The difference betweenmount
andshallowMount
is thatshallowMount
will only contain stubbed child components.
For more information on
Wrapper
,Component
,HTMLElement
, and other testing features please see Vue Test Utils.
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.