- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- 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.
Lab 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 BudgetFormcommand. 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 -- --hostwithin 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
BudgetFormcomponent. 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 dataspec found insrc/components/__tests__/BudgetForm.spec.js.If you would like to preview the solution, it can be found in the
soln/SolnBudgetComponentForm.spec.jsfile. As you learn about Vue Testing, you'll review certain objects and methods provided to you.In the upcoming tasks, use
shallowMountwith the component that you are testing as the argument.shallowMountreturns aWrapperobject. 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
wrapperthat you create contains the mounted and rendered Vue component that was passed toshallowMount.The variable
wrapperis of typeWrapperas defined inVue Test Utilsdocumentation.For this step, it is important to note that the
Wrapperobject has the propertyvmof typeComponent. You can access instance methods and properties of ourBudgetFormcomponent 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
methodsproperty of a component details all of the functions it is able to perform. TheBudgetFormcomponent hassubmitBudgetAmountForm()andsubmitItemForm()methods which are responsible for emitting the user input data to be displayed.In this step, you will test that calling
BudgetForm’ssubmitBudgetAmountFormemits theset-budget-amountevent with the correctbudgetAmountvalue.You will implement the
emits "set-budget-amount" event with correct payload when submitBudgetAmountForm method is calledspec found insrc/components/__tests__/BudgetForm.spec.js.If you would like to preview the solution, it can be found in the
soln/SolnBudgetComponentForm.spec.jsfile. -
Challenge
Testing the BudgetForm Component's submitItemForm Instance Method
The
methodsproperty of a component details all of the functions it is able to perform. For theBudgetFormcomponent, it hassubmitBudgetAmountForm()andsubmitItemForm()methods which are responsible for emitting the user input data to be displayed.In this step, you will test that
BudgetForm’ssubmitItemFormemits theadd-itemevent with the correctdescriptionanditemAmountvalues.You will implement the
emits "add-item" event with correct payload when submitItemForm method is calledspec found insrc/components/__tests__/BudgetForm.spec.js.If you would like to preview the solution, it can be found in the
soln/SolnBudgetComponentForm.spec.jsfile. -
Challenge
Testing BudgetForm Rendered HTML for Submitting Overall Budget Amount
The last part in the
BudgetFormcomponent 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 elementspec found insrc/components/__tests__/BudgetForm.spec.js.If you would like to preview the solution, it can be found in the
soln/SolnBudgetComponentForm.spec.jsfile. -
Challenge
Testing BudgetForm Rendered HTML for Submitting Budget Item
The last part in the
BudgetFormcomponent 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 elementspec found insrc/components/__tests__/BudgetForm.spec.js.If you would like to preview the solution, it can be found in the
soln/SolnBudgetComponentForm.spec.jsfile. -
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.propsto predict what props should be expected and defined. - How
optionson aComponentobject can be used to access any of theoptionsthat 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 aWrapperArrayof all matching Vue Components. - Using
mount({Some Component})instead ofshallowMount({Some Component}). The difference betweenmountandshallowMountis thatshallowMountwill only contain stubbed child components.
For more information on
Wrapper,Component,HTMLElement, and other testing features please see Vue Test Utils.
About the author
Real skill practice before real-world application
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.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.