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

182
Vistas
How to write unit test for AMD format JS code using jest

I am trying to write unit test for an audioPlayer written in AMD JS format. I was able to mock define() following a solution provided here But I am getting error for the following code snippet

    Object.defineProperty(HTMLMediaElement.prototype, "playing", {
        get: function() {
            return !!(
                this.currentTime > 0 && // audio at time 0 is not playing
                !this.paused &&         // audio that is paused is not playing
                !this.ended &&          // audio that is ended is not playing
                this.readyState > 2);   // audio that is not ready enough to play cannot be played
        }
    });

The error : ReferenceError: HTMLMediaElement is not defined

I tried to mock HTMLMediaElement via jest.spyOn like below:

    let playing;
    
    beforeEach(() => {
        playing = jest.spyOn(window.HTMLMediaElement.prototype, 'playing').
        mockImplementation(() => {});
    });
    

Is there a way to remove the error or mock the HTMLMediaElement element.

I also tried changing the testEnvironment property in the jest config from node to jsdom but that did not help.

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