Skip to content

How to understand a Jest or Vitest test suite codebase

Tests are executable documentation. A good suite shows how each module is meant to be called and what it should return, including edge cases the code alone doesn't make obvious. Knowing how mocks and setup files work keeps you from being misled by them.

How to tell it's Jest or Vitest test suite

  • jest.config.* or vitest.config.*, or a test block in vite.config.ts
  • Files named *.test.ts, *.spec.ts or folders called __tests__
  • jest.mock or vi.mock calls and a __mocks__ folder

Where things usually live

jest.config.ts / vitest.config.ts
Test environment, setup files, path aliases, coverage.
test/setup.ts
Global setup: matchers, fake timers, mocked globals.
**/__tests__/ or *.test.ts
The tests, colocated or in a separate folder.
__mocks__/
Manual mocks that replace modules in tests.
test/fixtures/
Sample data and files.

Conventions, not rules. Real projects rename and move things, so confirm against the repository.

Where to start reading

  1. The config and setup files, to know what every test inherits
  2. The tests for the module you're learning
  3. Mocks, to know which real code a test is not exercising

How a request moves through the code

  1. 1The runner loads the config and runs setup files.
  2. 2vi.mock and jest.mock calls are hoisted above imports, replacing modules before the test file loads them.
  3. 3Each describe and it block arranges inputs, calls the code, and asserts on the result.
  4. 4Hooks such as beforeEach reset state between tests.

Questions to ask about the repository

  • What behaviour do the tests for this module describe?
  • Which modules are mocked, and does that hide real integration paths?
  • Which important functions have no tests?

What trips people up

  • Mock calls are hoisted, so a mock written at the bottom of a file still applies to imports at the top.
  • A global mock in a setup file can make every test pass against fake behaviour.