Vitest — Fast Unit Testing for TypeScript

Sanjeev SharmaSanjeev Sharma
1 min read

Advertisement

Vitest is the modern TypeScript testing framework. 5x faster than Jest with better ergonomics.

Setup

npm install -D vitest
import { describe, it, expect } from "vitest";

describe("Calculator", () => {
  it("adds numbers", () => {
    expect(1 + 1).toBe(2);
  });

  it("fails on invalid input", () => {
    expect(() => calculate("invalid")).toThrow();
  });
});

Async Testing

it("fetches user", async () => {
  const user = await getUser(1);
  expect(user.name).toBe("Alice");
});

Mocking

import { vi } from "vitest";

vi.mock("./db", () => ({
  getUser: vi.fn().mockResolvedValue({ id: 1, name: "Alice" }),
}));

FAQ

Q: Vitest vs Jest? A: Vitest faster, better TypeScript support, modern syntax.

Q: Snapshot testing? A: Yes, use toMatchSnapshot().


Vitest is the future of TypeScript testing. Fast, ergonomic, modern.

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro