0

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?

1 Answer 1

0

I got an answer:

That one happens because Radix uses pointer events, and happy-dom/jsdom don’t fully support them.

your workaround adding those small polyfills in your setup file is totally fine.

everyone using Radix or shadcn in Vitest does something similar.

If you ever want to avoid polyfills, the only real alternative is running your tests in a real browser (like Vitest browser mode or Playwright), but that’s heavier.

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.