Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free lesson
Why and What to Test
Why and What to Test
In this lesson — part of Testing Fundamentals — you'll learn why and what to test in TypeScript and why it matters in real work.
Why it matters
Tests prove your code works and keep it working as you change it.
Key ideas
- What to test
- Arrange-Act-Assert
- Running a test suite
- Good vs. brittle tests
In practice
Here's how it looks in idiomatic TypeScript:
import { describe, it, expect } from "vitest";
function add(a: number, b: number) { return a + b; }
describe("add", () => {
it("sums two numbers", () => {
expect(add(2, 3)).toBe(5);
});
});
TypeScript note: Vitest (and Jest) run TypeScript tests directly and infer expect matcher types, so a wrong-typed assertion fails at compile time too.
Try it yourself
Exercise: In TypeScript, write three tests for a function that reverses a string.
Recap
You now understand why and what to test and can apply it in TypeScript. Mark this lesson complete and continue to the next one.
