Setup Source Template for Vue 3 – Part 2

Tram Ho

Introduce

Hello everyone, in part 1 , I gave an overview of the source code and basic setup of Vue 3 + Vite. In this article, I continue to learn about and setup vue-router , pinia and Vitest

Content

This is the folder structure of the project after setup.

Setting

In this section, I will guide you to setup:
🍍 State Management via Pinia , Vue Router
βš™οΈ Unit Testing with Vitest

Now we will install vue-router and pinia from PNPM into our project.

Vue Router

vue-router is the official router for Vue.js. We will need to install version 4 compatible with Vue 3 :

Create a routes folder and create an index.ts . file

Import routes into main.ts . file

Pinia

Pinia is one of the newest projects to emerge from the Vue ecosystem and it is the new official State Management tool for Vue.js applications. Its api is very similar to Vuex (its predecessor) and it is designed to be faster and lighter.

Create a folder stores and create a file auth.ts

Import pinia into main.ts . file

Vitest

Vitest is a very fast unit test framework powered by Vite.

Vitest requires Vite >=v3.0.0 and Node >=v14

add test script to package :

We will try to write some tests in the sample.spec.ts file to check whether the test is working or not.

Then we run pnpm run test and the result is: Screenshot 2023-02-02 at 23.18.42.png

End

In this part we have learned and setup vue-router , pinia and testing . In the next part we will learn about and set up eslint , prettierrc and the commitlint rule .

The source

https://github.com/trungpham71198/vue-template/tree/feat/chapter-2

Share the news now

Source : Viblo