I'm testing components built with shadcn/ui, which uses Radix UI under the hood. When running UI interaction tests using Vitest and @testing-library/user-event, I keep getting the following error:
target.hasPointerCapture is not a function
Here’s what I’ve tried so far:
- Changed the test environment to happy-dom in vitest.config.mts
- Initialized userEvent with pointerEventsCheck: 0:
const user = userEvent.setup({ pointerEventsCheck: 0 });
The error still occurs.
The only workaround that (I found) seems to work is polyfilling the missing DOM APIs in vitest config :
beforeAll(() => {
if (!Element.prototype.hasPointerCapture) {
Element.prototype.hasPointerCapture = vi.fn().mockReturnValue(false);
}
if (!Element.prototype.setPointerCapture) {
Element.prototype.setPointerCapture = vi.fn();
}
if (!Element.prototype.releasePointerCapture) {
Element.prototype.releasePointerCapture = vi.fn();
}
if (!Element.prototype.scrollIntoView) {
Element.prototype.scrollIntoView = vi.fn();
}
});
This centralizes the missing DOM APIs so I don’t have to repeat them in every test file.
Question: Is there any other approach (other than adding these manual polyfills) to avoid hasPointerCapture–related errors when testing Radix UI / shadcn components with Vitest and userEvent?