Skip to content
Learn with RV – Tech Blog
Learn with RV – Tech Blog

#testautomation #qa #programming #linux #devops

  • Home
  • Who am I?
  • 1-on-1 Mentoring
  • Test Automation Incubator
  • 24 Testimonials
  • YouTube
  • LinkedIn
  • Contact
Learn with RV – Tech Blog

#testautomation #qa #programming #linux #devops

June 25, 2026June 25, 2026

Unit Testing in JavaScript: Getting started with Vitest

Unit testing is one of the easiest ways to make your JavaScript code more reliable. A unit test checks a small piece of code in isolation and confirms that it behaves as expected.

In this article, we will set up a very simple testing stack using Vitest then write our first unit tests for a basic application.

Why Unit Testing?

When building a website, even a simple one, you usually have small pieces of logic:

  • calculating totals
  • validating form input
  • changing text on the page
  • reacting to button clicks
  • updating HTML elements

Unit tests help you make sure these small pieces continue to work when you change your code later.

The tech stack

For this setup, we will use:

  • Vitest: a fast JavaScript testing framework
  • jsdom: a browser-like DOM environment for Node.js

Vitest runs our tests. jsdom gives us access to browser-like objects such as document, window, and HTML elements while running tests outside a real browser.

Installing packages

First, install the dependencies:

npm install -D vitest jsdom

Then update your packages.json

vitest-package
Now you can run your test with

npm test

Testing a function

Let’s start with a pure JavaScript function. A pure function is easy to test because it does not depend on the browser, HTML, or external state.

Create a file called cart.js

cart-vitest

Now create a test file called cart.spec.js

This test checks that when the price is 50 and the quantity is 3, the total is 150.

Run the test

npm test

Everything works and Vitest shows the test results

vitest-results

Enjoyed this article?
I share more practical automation tips on YouTube and LinkedIn.

Need structured guidance instead of learning alone?
I offer 1-on-1 mentoring – learn more → HERE

Or email me at iamqarv [at] gmail [dot] com

Post Views: 81

Related

Share this article:
QA #qa

Post navigation

Previous post
Next post

Recent Posts

  • Using npm –prefix to Run Scripts from a Nested package.json
  • Unit Testing in JavaScript: Getting started with Vitest
  • Fail Fast in Playwright with maxFailures
  • Cleaner asserts in Grafana k6 load tests using expect
  • AI: What Happens When an AI’s Context Window Gets Full?

Recent Comments

  1. Paul on Web Accessibility: A step-by-step guide to Testing with pa11y
  2. Automated Tests for website Accessibility with Axe and TestCafe - Learn with RV - Tech Blog on How to generate E2E TestCafe Framework in seconds
  3. RV on Exploring Faker.js: A Powerful Tool for Generating Realistic Random Test Data
  4. Adrian Maciuc on Exploring Faker.js: A Powerful Tool for Generating Realistic Random Test Data
  5. Nick on Cypress vs Playwright vs Testcafe – which framework is faster?

Archives

  • July 2026
  • June 2026
  • May 2026
  • April 2026
  • March 2026
  • February 2026
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • March 2025
  • February 2025
  • January 2025
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023

Categories

  • AI
  • k6
  • Linux
  • Programming
  • QA
  • Tools
  • Uncategorized
©2026 Learn with RV – Tech Blog | WordPress Theme by SuperbThemes