This is actually a very common issue when testing modern web applications, especially with component libraries. Here are the main causes:
Common Causes
1. Modern UI Libraries
Many popular component libraries use ResizeObserver internally:
- Headless UI
- Radix UI
- Material-UI
- Ant Design
- Chart libraries (Chart.js, D3)
- Virtual scrolling libraries
2. Svelte-specific Libraries
Several Svelte ecosystem packages use ResizeObserver:
- Svelte actions that monitor element dimensions
- Layout components
- Responsive utilities
- Some SvelteKit adapters
3. Your Own Code
You might be using ResizeObserver in:
- Custom Svelte actions
- Responsive components
- Dynamic layout calculations
Why It's So Common
The ResizeObserver API is relatively new (became widely supported around 2020) and incredibly useful for responsive design. However, testing tools haven't fully caught up:
- Node.js doesn't have browser APIs by default
- JSDOM (the DOM implementation Vitest uses) doesn't include ResizeObserver
- Many developers don't realize their dependencies use it until tests fail
How to Identify the Source
If you want to track down what's using ResizeObserver:
// In your test setup
global.ResizeObserver = class ResizeObserver {
constructor(callback) {
console.trace('ResizeObserver created'); // This will show you the stack trace
this.callback = callback;
}
// ... rest of mock
};
This scenario is common enough that many testing setup guides now include ResizeObserver mocking as a standard step. It's one of those "gotchas" that catches most developers at least once when setting up component testing.