Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

638
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda