Skip to main content
This covers effective methods for debugging tests written with Vitest.

Vitest Debugging Methods

Sonamu uses Vitest as its test framework. It provides various debugging methods.

Using VSCode Debugger

1. launch.json Configuration

.vscode/launch.json:

2. Running the Debugger

  1. Open test file
    • Open the .test.ts file you want to debug
  2. Set breakpoints
    • Click to the left of the line number to show a red dot
  3. Start debugger
    • Press F5 or select “Debug Current Test File” from the “Run and Debug” panel
  4. Debug
    • When paused at a breakpoint, inspect variables
    • Use F10 (Step Over), F11 (Step Into), etc.

Console Logging

Using console.log

The simplest debugging method:

Structured Logging

Running Specific Tests

test.only

describe.only

Running Specific Tests via CLI

Watch Mode

Auto Re-run

When you modify a file, related tests are automatically re-run.

Watch Mode Shortcuts

During test execution:
  • a: Re-run all tests
  • f: Re-run failed tests only
  • t: Filter specific tests
  • q: Quit

UI Mode (vitest —ui)

Running

Opens http://localhost:51204/__vitest__/ in the browser Features:
  • Visualize test hierarchy
  • Filter failed tests
  • Re-run individual tests
  • Check code coverage
  • View console output

Error Debugging

Detailed Error Messages

Checking Errors with try-catch

Debugging Async Code

Checking async/await

Increasing Timeout

Debugging Mocks

Checking Mock Calls

Checking Mock Reset

Checking Database State

Query Logging

Checking Data After Test

Coverage Debugging

Checking Coverage

Finding Uncovered Code

Vitest Configuration Debugging

Checking Configuration

vitest.config.ts:

Checking Environment Variables

Debugging Parallel Execution

Disabling Isolation Mode

Some tests may have issues when running in parallel:

Sequential Execution

Snapshot Debugging

Updating Snapshots

Checking Snapshots

Type Check Debugging

Checking Type Errors

Type Tests

Test Isolation Issues

Checking beforeEach/afterEach

Memory Leak Debugging

Checking Memory Usage

Checking Resource Cleanup

Best Practices

1. Test in Small Units

2. Meaningful Test Names

3. AAA Pattern (Arrange-Act-Assert)

4. Sufficient Information on Failure