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

185
Views
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
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!