Structure tests with describe/it, mock modules with vi.mock, measure coverage with V8/Istanbul, use snapshot testing, and write in-source tests.
0 / 5 completed
1 / 5
What is the structure of a Vitest test file using describe and it?
describe/it structure: Vitest is intentionally Jest-compatible. describe groups tests logically for better output organisation and allows scoped beforeEach/afterEach hooks. it and test are aliases. it.only and it.skip control which tests run, enabling focused debugging without modifying other tests.
2 / 5
What does vi.mock() do in Vitest and when should it be used?
vi.mock(): hoisted to the top of the file by Vitest (like Jest's jest.mock). vi.mock("axios") auto-mocks all exports as vi.fn(). The factory form provides explicit implementations. Use vi.spyOn(obj, "method") to mock a method on an existing object while retaining the ability to call through to the original. Reset mocks between tests with vi.clearAllMocks().
3 / 5
How does Vitest coverage work and which providers does it support?
Vitest coverage:vitest run --coverage generates reports. V8 coverage uses Node.js's built-in profiler — fast but may miss branches in heavily transformed code. Istanbul instruments the source at the AST level — slower but more accurate, especially for conditional expressions. Configure thresholds with coverage.thresholds.lines: 80 to fail CI when coverage drops.
4 / 5
What is snapshot testing in Vitest and when should it be updated?
Snapshot testing: snapshots are stored in __snapshots__/ directories and committed to version control. When intentional changes are made, update snapshots with vitest --update-snapshots. Use inline snapshots (toMatchInlineSnapshot()) for small values to keep snapshots co-located with tests. Review snapshot diffs in PRs to catch unintended rendering changes.
5 / 5
What is in-source testing in Vitest and what is its use case?
In-source testing: useful for testing pure utility functions or internal implementation details without exporting them. The import.meta.vitest guard means the test code is tree-shaken from production builds. Enable with includeSource: ["src/**/*.ts"] in vitest.config. Best reserved for small, co-located unit tests — larger test suites belong in separate files.