Home / Companies / Mergify / Blog / Post Details
Content Deep Dive

Vitest's `threads` pool is fast. It is also why your suite leaks state.

Blog post from Mergify

Post Details
Company
Date Published
Author
-
Word Count
1,066
Language
English
Hacker News Points
-
Summary

Vitest's default threads pool, which optimizes for speed by reusing module states across tests, can lead to state leakage issues, causing test failures that appear as logic bugs. This configuration is useful for performance as it amortizes the cost of module loading, but it disrupts the assumption of test file isolation that many engineers hold, leading to tests inadvertently sharing state. A common issue is when a module-level Set in a Vitest worker thread is mutated by one test and unexpectedly affects another, which is often caught by tools like Mergify Test Insights. Solutions to this problem include using a slower per-file process isolation with forks, isolating specific files with poolMatchGlobs while maintaining thread usage for the rest, or refactoring to avoid shared states entirely. While manual triage often misidentifies the source of the failure, Mergify's integration with Vitest can identify and tag cross-file dependencies, providing a clearer picture of the issue and aiding in decision-making between configuration changes or code refactoring.