DEV Community

Arhan Khan
Arhan Khan

Posted on

Vitest for Beginners: Testing That Feels Like Vite — Fast, Simple, and Familiar

Vitest is a test runner built for the Vite era. If you have ever waited for Jest to start, fought with its configuration, or wished your tests ran as fast as your dev server, Vitest is the fix. It is fast, it works with Vite out of the box, and its API is so close to Jest that most developers switch in an afternoon.

The key idea is simple: Vitest reuses Vite's transform pipeline, so your tests run with the same speed and the same module handling as your app. Hot module reloading works in tests, TypeScript works without extra setup, and the watch mode updates results instantly as you type.

// sum.js
export function sum(a, b) {
  return a + b;
}

// sum.test.js
import { describe, it, expect } from "vitest";
import { sum } from "./sum.js";

describe("sum", () => {
  it("adds two numbers", () => {
    expect(sum(2, 3)).toBe(5);
  });

  it("handles negative numbers", () => {
    expect(sum(-1, 1)).toBe(0);
  });
});
Enter fullscreen mode Exit fullscreen mode
npm install -D vitest
npx vitest
Enter fullscreen mode Exit fullscreen mode

Getting started is simple. Install Vitest, write a file ending in .test.js, and run npx vitest. If your project already uses Vite, there is almost nothing to configure — Vitest reads your Vite config automatically. The docs include a migration guide from Jest that covers the common cases in a few pages.

Vitest is for JavaScript and TypeScript developers who use Vite, beginners learning testing for the first time, and teams who want tests that run in milliseconds instead of seconds. It works with React, Vue, Svelte, and plain JavaScript, and it supports the same describe, it, and expect style you already know.

The limits are honest. Vitest is newer than Jest, so some older plugins and edge cases are still catching up. If your project does not use Vite, Jest may still be the simpler choice, and very large legacy test suites can need small changes to migrate.

Start today with one thing: install Vitest in any project you have and write three tests for one small function. Watch them run in under a second. In a week you will have tests for your whole app — and you will never go back to waiting for a slow test runner.

Top comments (0)