Test Frameworks
Compare Vitest, Jest, Mocha, Playwright, and others to choose the right tool for your project.
What you'll learn
- What a test framework provides: runner, assertions, mocks, reporters
- How Vitest, Jest, and Mocha differ in philosophy and speed
- Choosing a framework based on your runtime and ecosystem
- How assertion libraries and mock libraries plug in
Concept
What a Framework Does
A test framework handles the mechanics so you can focus on behavior. It discovers test files, runs them in isolated contexts, provides assertion functions, offers mocking utilities, watches files for re-runs, and reports results in a readable format. Choosing a framework is largely about which trade-offs — speed, ecosystem, configuration — fit your project.
The Big Three for JavaScript
Jest was created by Facebook and became the default for React projects. It is batteries-included (assertions, mocks, snapshots, coverage) with zero config, but its legacy architecture can be slow on large suites. Vitest is a modern, Vite-native framework with a Jest-compatible API that runs tests through esbuild and is dramatically faster for E2E and large projects. Mocha is a minimal, flexible runner that leaves assertions and mocks to libraries you choose — powerful but more setup.
Browser and Specialized Frameworks
For browser automation, Playwright and Cypress lead. For Node-only unit tests, node:test (built into Node 18+) needs no dependencies at all. For benchmarking, Benchmark.js and Vitest's bench API measure performance rather than correctness. Match the framework to the layer you are testing.
Making the Choice
For a new Vite or Astro project, Vitest is the natural choice — it shares your Vite config and transforms code identically to production. For an existing Create React App or large Jest codebase, Jest remains pragmatic. Avoid mixing frameworks without reason; a single, well-configured framework keeps the team's mental model simple and CI fast.
In Practice
When evaluating a framework, try writing five real tests against your actual codebase rather than a toy example — that reveals the real configuration pain (TypeScript paths, CSS imports, DOM globals). Check the framework's watch-mode speed on your largest test file, because that determines your daily feedback loop. Look at the reporter output: can you read a failure and know where to look? Does it support the coverage provider your CI needs? Ecosystem matters too — Testing Library, MSW, and Playwright all integrate cleanly with both Vitest and Jest, so you are not locked in. If you later migrate from Jest to Vitest, the API compatibility means most tests port with an import change, making the switch low-risk when the speed payoff justifies it.
Example
// The same test looks almost identical in Jest and Vitest.
// jest: const { describe, it, expect } = require("@jest/globals");
import { describe, it, expect } from "vitest"; // or "jest"
function isEven(n) {
return n % 2 === 0;
}
describe("isEven", () => {
it("returns true for even numbers", () => {
expect(isEven(4)).toBe(true);
});
it("returns false for odd numbers", () => {
expect(isEven(7)).toBe(false);
});
});
// Run with: npx vitest (Vitest) or npx jest (Jest)
Vitest intentionally mirrors Jest's API, so most tests port between the two with only an import change. Pick based on speed and toolchain fit, not syntax.
Try it
- Code Diff
Compare framework outputs when migrating between Jest and Vitest.
- JSON Formatter
Read the JSON test result reports frameworks emit for CI.
Common mistakes
The mistake
Picking a framework because it is popular, not because it fits
The fix
Evaluate against your real project: a Vite app wants Vitest, a Node service may want node:test, a legacy CRA app may stay on Jest.
The mistake
Mixing assertion styles within one suite
The fix
Standardize on one assertion library (expect from Vitest/Jest, or assert from node). Mixed styles confuse readers and tooling.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.