# Testing Fundamentals with Vitest

> Learn what to test, how to structure tests, and how to write fast unit tests with Vitest for business logic. Free notes, key concepts, resources and a mini...

Canonical: https://www.internhack.in/roadmaps/tracks/full-stack/testing-basics
Last updated: 2026-11-28

Learn what to test, how to structure tests, and how to write fast unit tests with Vitest for business logic.

Intermediate · about 5 hours · difficulty 2/5

Learn first: [TypeScript Fundamentals](https://www.internhack.in/roadmaps/tracks/full-stack/typescript), [npm, Packages and Build Tooling](https://www.internhack.in/roadmaps/tracks/full-stack/npm-and-tooling)

Tests let you change code without fear. In interviews and internships, a project with meaningful tests stands out immediately.

### The testing pyramid (or trophy)

- **Unit tests**: pure functions and services. Fast and many.
- **Integration tests**: several pieces together, like an API route with a real database.
- **End-to-end tests**: a real browser clicking through the app. Slow, so keep them for critical flows.

Many full-stack teams find integration tests give the most confidence per test.

### Writing good tests

- Arrange, act, assert. One behaviour per test, with a name that reads like a sentence.
- Test behaviour, not implementation details, so refactors do not break tests.
- Cover edge cases: empty lists, invalid input, boundary values (CGPA exactly 7.0 for an eligibility rule).

### Vitest

**Vitest** is fast, works with TypeScript and ESM out of the box, and has a Jest-compatible API (describe, it, expect, vi.fn). Use watch mode while coding and coverage reports to find untested branches, not as a target to game.

### Pitfalls

- Mocking so much that the test only checks your mocks.
- Tests that depend on each other or on the current date without controlling it.
- Chasing 100 percent coverage instead of testing risky logic.

You are ready to move on when your eligibility and fee calculation logic has Vitest tests covering normal cases, edge cases and errors, all running in under a few seconds.

## Key concepts

- **Unit, integration and E2E**
- **Arrange, act, assert**
- **Vitest basics**
- **Mocks, spies and fakes**
- **Fake timers and dates**
- **Coverage reports**
- **Jest**

## Mini project

Write a placement eligibility module (CGPA cutoff, allowed branches, backlog limit, graduation year) and cover it with at least 15 Vitest tests, including edge cases.

## Check yourself

Your tests break every time you rename an internal helper even though behaviour is unchanged. What does that tell you about how they are written?

## Free resources

- [Vitest Guide](https://vitest.dev/guide/) (Vitest)
- [The Testing Trophy and Testing Classifications](https://kentcdodds.com/blog/the-testing-trophy-and-testing-classifications) (Kent C. Dodds)
- [Jest](https://jestjs.io/docs/getting-started) (Jest)

Previous: [Web Security Essentials](https://www.internhack.in/roadmaps/tracks/full-stack/web-security) · Next: [Component Testing with React Testing Library](https://www.internhack.in/roadmaps/tracks/full-stack/component-testing)

## More in Testing the Whole Stack

- [Testing Fundamentals with Vitest](https://www.internhack.in/roadmaps/tracks/full-stack/testing-basics)
- [Component Testing with React Testing Library](https://www.internhack.in/roadmaps/tracks/full-stack/component-testing)
- [API Integration Tests with a Real Database](https://www.internhack.in/roadmaps/tracks/full-stack/api-integration-testing)
- [End-to-End Testing with Playwright](https://www.internhack.in/roadmaps/tracks/full-stack/e2e-playwright)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Full-Stack Developer Roadmap](https://www.internhack.in/roadmaps/tracks/full-stack)
