๐‡๐จ๐ฐ ๐ญ๐จ ๐ ๐ž๐ญ ๐ฌ๐ญ๐š๐ซ๐ญ๐ž๐ ๐ฐ๐ข๐ญ๐ก ๐“๐ž๐ฌ๐ญ๐ข๐ง๐  ๐š ๐ซ๐ž๐š๐œ๐ญ ๐š๐ฉ๐ฉ ๐ฎ๐ฌ๐ข๐ง๐  ๐•๐ข๐ญ๐ž๐ฌ๐ญ + ๐‘๐ž๐š๐œ๐ญ ๐“๐ž๐ฌ๐ญ๐ข๐ง๐  ๐‹๐ข๐›๐ซ๐š๐ซ๐ฒ and ๐•๐ข๐ญ๐ž ๐ญ๐จ๐จ๐ฅ๐ข๐ง๐ ?

Max Shahdoost
3 min readMay 8, 2023

Vitest and Unit Testing

In this post, I am going to summarize how you can easily get started using cutting-edge tools to test your react/next frontend application so we are going to use ๐‘๐ž๐š๐œ๐ญ, ๐“๐ฒ๐ฉ๐ž๐’๐œ๐ซ๐ข๐ฉ๐ญ, ๐•๐ข๐ญ๐ž, ๐•๐ข๐ญ๐ž๐ฌ๐ญ, ๐‘๐ž๐š๐œ๐ญ ๐“๐ž๐ฌ๐ญ๐ข๐ง๐  ๐‹๐ข๐›๐ซ๐š๐ซ๐ฒ and as you see I omitted ๐‰๐ž๐ฌ๐ญ from the setup since it needs more boilerplate for setup and because of behind scene compilation stuff it is much slower than Vitest but you donโ€™t need to worry at all, they are almost the same in terms of 95% of their features and there is no issue about that.

1- First of all, you need to install these packages after you set up a Vite application using React library and TypeScript.

npm i vitest @testing-library/react @testing-library/user-event @testing-library/jest-dom @testing-library/dom jsdom -D

2- Create a vitest.config.ts file in the root of your project like the image below.

import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";

export default defineConfig({
plugins: [react()],
test: {
/* for example, use global to avoid globals imports (describe, test, expect): */
globals: true,
environment: "jsdom",
setupFiles: "./src/test/setup.ts",
// you might want to disable it, if you don't have tests that rely on CSS
// since parsing CSS is slow
css: false,
},
});

If you want to make some customization based on your needs, you can read the comprehensive documentation here:

3- Create a test folder in your src folder and create a file called setup.ts and add this line of code to it:

import "@testing-library/jest-dom";

By adding this we will be able to access jest functions like describe and etc in our test files.

4- Add the test script inside your package.json:

Test and coverage scripts
    "dev": "vite",
"test": "vitest",
"coverage": "vitest run --coverage",
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"

๐‚๐จ๐ง๐ ๐ซ๐š๐ญ๐ฎ๐ฅ๐š๐ญ๐ข๐จ๐ง๐ฌ, ๐ฒ๐จ๐ฎ ๐š๐ซ๐ž ๐ ๐จ๐จ๐ ๐ญ๐จ ๐ ๐จ ๐ง๐จ๐ฐ! ๐ฐ๐š๐ฌ๐งโ€™๐ญ ๐ข๐ญ ๐ž๐š๐ฌ๐ฒ ๐ซ๐š๐ญ๐ก๐ž๐ซ ๐ญ๐ก๐š๐ง ๐‰๐ž๐ฌ๐ญ?!

Remember that our test runner is Vitest here but we will need to use jsdom environment and jest-dom to be able to access DOM-related functionalities inside our test files.

Now letโ€™s write our first tests and enjoy!

First unit tests

I hope this could help you get started with testing your applications very fast and if so, please support by sharing.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Max Shahdoost
Max Shahdoost

Written by Max Shahdoost

A highly passionate and motivated Frontend Engineer with a good taste for re-usability, security, and developer experience.

No responses yet

Write a response