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

304
Visualizações
How to create an automatic mock for built-in objects? (Jest, Vanilla JS)

I know that with Jest you can create an automatic class mock for an ES6 class. You can do that by calling jest.mock() with the filename as parameter, like shown below:

import ClassThatShouldBeMocked from "./path/to/the/file.js";

jest.mock("./path/to/the/file.js");

// ClassThatShouldBeMocked is now mocked

But in my case I want to mock built-in objects like HTMLElement or CanvasRenderingContext2D. How do I get the path of such built-in objects? Is there another way to achieve an automatic mock in this case?


Currently I'm using jest.spyOn() to create an spy function for each method of this class, that is called in my code. This works but takes up much more time and effort, when I have to call many functions and methods every time.


Edit:
Im currently using:

  • jest@28.1.0
  • jest-environment-jsdom@28.1.0
  • babel-jest@28.1.0
about 4 years ago · Juan Pablo Isaza
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