Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

311
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda