Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

631
Views
How to substitute the PointerEvent in Jest? (PointerEvent is not defined)

Problem

In my react app I test for different event constructors in a switch case statement. Something like that:

switch (event.constructor) {
    case String:
        doSomething()
        break;
    case Array:
        doSomething()
        break;
    case Date:
        doSomething()
        break;
    case PointerEvent:
        doSomething()
        break;
    default:
        doSomething()
        break;

If I test this setup with jest it always throws me the error ReferenceError: PointerEvent is not defined. I can fix this problem by setting PointerEvent to window.PointerEvent. But the event in my jest environment has the type MouseEvent.

Question

How can I set the PointerEvent as an alias for the MouseEvent during testing? Or is there even a way to import an execute a PointerEvent in jest?

System

"@mui/material": "^5.6.3",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^13.0.0",
"@testing-library/user-event": "^13.2.1",
"react": "^18.0.0",
"react-dom": "^18.0.0",
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you look at this issue, you can see that jsdom appears to not implement PointerEvent

You can work around this this though

class MockPointerEvent {}

describe("PointerEvent", () => {
  beforeEach(() => {
    global.window.PointerEvent = MockPointerEvent as any;
  });

  it("confirms pointer event", () => {
    const pointerEvent = new PointerEvent("");

    expect(pointerEvent).toBeTruthy();
  });
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!