Vitest
How to use Vue with Vitest - free starter
This article will teach you how to integrate Vitest - recommended by Vite testing tool with your project.
Lets see how to use Vitest with MDB 5 for testing across our layout, components, and utilities.
Prerequisites
Before starting the project make sure to install the following utilities:
Creating a new project
First, we need to create a new Vite project.
Step 1
Init the project and choose vue framework. You can add a name for your project as we did in example below
Step 2
Navigate to app's directory.
Step 3
Install dependencies.
Step 4
Setup MDB.
Step 5
Import CSS. Add the following line at the beginning of your main.ts
file:
Step 6
Import Font Awesome and Roboto font. Add the following lines in public/index.html file inside
head
section:
Step 7
Enable sourcemaps during development.
Step 8
Launch the application.
Installation & usage
Step 1
Install Vitest.
Step 2
Create simple component in src
directory so we will able to test most important features.
Step 2
Create test
folder inside root directory and put ExampleComponent.test.ts
inside and write basic test checking if the componet renders properly.
Step 3
But we will get an error because unrecognized third-party library. For that we have to mock MDBVue UI Kit. Add vi.mock
above our test.
Step 4
Beyond that Vitest needs an environment for proper working. You can use one of couple available (more about in Vitest documentation) but for now let's try with happy-dom
.
Step 5
After installation all we need to do is add commented line at the top of our test.
Step 6
For testing more advanced things like props or another elements in our component we got to use more mocking functions. For example if there things using Vue's slots
, then we have to use template
element in vi.mock
function. This is an example test using a few more advanced mocks.
More about Vitest
For more information, see Vitest page. There you can read about other options, best practices and testing.