Testing

Vitest

A fast Vite-based test runner with a Jest-compatible API and native TypeScript and ESM support.

What it is

Anthony Fu and Vladimir (together with the Vue and Vite team) created Vitest in 2021. It reuses the same config and plugins as Vite, so tests see the same aliases and transforms as the app.

The API nearly matches Jest (describe, it, expect, vi.fn), so migration is easy. It has watch mode with instant reruns, native TypeScript and ESM with no setup, coverage via v8 or istanbul, and a browser mode.

Vitest fits projects on Vite, Vue, Svelte and React. It is faster than Jest in many scenarios thanks to parallelism and reusing the Vite cache.

Use it when

  • A Vite project: one config for build and tests.
  • You need native TypeScript and ES module support without Babel.
  • Migrating from Jest with minimal changes.

Avoid it when

  • React Native, where tooling is centered on Jest.
  • A non-Vite project with many Jest-specific plugins.

Key concepts

Vite config reuse
Aliases, plugins and env variables come from vite.config.
vi utility
vi.fn, vi.mock and vi.useFakeTimers for mocks and timers.
Watch mode
Reruns only the tests affected by a change.
In-source testing
Tests can be written next to the code in the same file.
Browser mode
Run tests in a real browser for components.

Example

The same test as in Jest, with vi.fn().

Interview questions

  1. 1. How does Vitest differ from Jest?

    Vitest runs on Vite, natively understands ESM and TypeScript and uses the build config. Jest is older with a bigger ecosystem but needs extra setup for ESM.

  2. 2. How do you migrate tests from Jest?

    Replace jest with vi in mocks, add imports from vitest or enable globals. Most expect matchers work unchanged.

  3. 3. What is in-source testing?

    The ability to write tests in the same file as the code, inside an if (import.meta.vitest) block. The block is stripped from the build.

  4. 4. How do you collect coverage?

    Run vitest run --coverage with the v8 or istanbul provider. The report shows which lines and branches ran.

Interview questions from the bank

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).