Vitest
Vitest is a Vite-native test runner with first-class TypeScript. It transpiles tests through Vite (esbuild / rolldown) and types its APIs. It can also assert types with expectTypeOf when you turn that on.
Detailed Explanation
Vitest understands .ts and .tsx because the same pipeline that bundles your app strips types. That pipeline is transpile-only by default. A type error in a test file will not fail vitest unless you add a typecheck project (vitest --typecheck, or a typecheck block in the config that runs tsc / vue-tsc). Treat vitest as the runtime runner and vitest --typecheck or tsc --noEmit as the type gate.
Imports are ESM. tsconfig paths are picked up from the Vite / Vitest config more readily than in Jest. Still confirm resolve.alias if you do not use the Vite plugin that reads tsconfig.
vi.fn() is typed similarly to jest.fn(). Prefer vi.fn<typeof impl>() or vi.mocked(impl). vi.hoisted has types for values you need before vi.mock hoists. Mocked modules can be typed with import type plus vi.mocked.
expectTypeOf(value).toEqualTypeOf<T>() and .toMatchTypeOf<T>() are type assertions. They are compiled away or run in the typecheck project depending on setup. Use them next to generic utilities. They are not a substitute for tsd if you need expectError on a statement that should not compile; Vitest's typecheck mode can do some of that with // @ts-expect-error in files it includes.
JSX / React tests need the same jsx and types as the app (vite/client, vitest/globals if you use globals). A second tsconfig for tests that extends the app and adds vitest/globals is the usual split.
Vitest and Jest share a matcher vocabulary on purpose. The TypeScript difference is the Vite pipeline and expectTypeOf. Pick one runner per repo. Dual types (@types/jest plus vitest/globals) will collide on expect.
See the JavaScript Vitest page for runner behavior. Here: typecheck is opt-in, mocks need generics, expectTypeOf pins utilities.
Code Examples
1. Typed vi.fn.
// 1. Typed vi.fn.
type User = { id: string };
type Find = (id: string) => Promise<User | undefined>;
const find = vi.fn<Find>();
find.mockResolvedValue({ id: "u1" });2. expectTypeOf for a utility.
// 2. expectTypeOf for a utility.
import { expectTypeOf } from "vitest";
type Id = Pick<{ id: string; extra: number }, "id">;
expectTypeOf<Id>().toEqualTypeOf<{ id: string }>();3. Typecheck is a separate command in CI.
// 3. Typecheck is a separate command in CI.
// package.json script idea:
// "test": "vitest run"
// "typecheck": "tsc --noEmit && vitest --typecheck"Common Gotchas
Default `vitest` does not type-check
green tests can sit next to red squiggles. Add a typecheck step.
`vi.fn()` without a type is an `any` factory
pass the function type.
`@types/jest` plus Vitest globals fight over `expect`
do not install both in one project.
`expectTypeOf` in a file that is only transpiled does nothing useful
run it under `--typecheck` or `tsc`.