I am trying to step up my Jest skills and working through Wes Boss classic 30 Vanilla JS projects in 30 Days. I thought I would do them all in LWCs with Jest for a challenge.

I built my Drum Kit and it works like a charm. But I can't figure out how to test this thing.

The root cause seems to be from calling .play() on an audio file.

When I run the test I get an error in the console 'console.error Error: Not implemented: HTMLMediaElement.prototype.play'

Any advice on the best way forward to test this LWC would be appreciated and any suggestions about material to help with Jest mastery.

Here is my HTML file.

    <div class="container">
        <div class='key' data-key="65" name="a" ontransitionend={removeTransition}>
            <span class="sound">Clap</span>
        <div class="key" data-key="83" ontransitionend= {removeTransition}>
            <span class="sound">hihat</span>
        <div class="key" data-key="68" ontransitionend={removeTransition}>
            <span class="sound">kick</span >
        <div class="key" data-key="70" ontransitionend={removeTransition}>
            <span class="sound">openhat</span>
        <div class="key" data-key="71" ontransitionend={removeTransition}>
            <span class="sound">boom</span>
        <div class="key" data-key="72" ontransitionend={removeTransition}>
            <span class="sound">ride</span>
        <div class="key" data-key="74" ontransitionend={removeTransition}>
            <span class="sound">snare</span>
        <div class="key" data-key="75" ontransitionend={removeTransition}>
            <span class="sound">tom</span>
        <div class="key" data-key="76" ontransitionend={removeTransition}>
            <span class="sound">tink</span>


Here is my JS file.

import {LightningElement} from 'lwc';
import Boom from '@salesforce/resourceUrl/sounds_boom';
import Clap from '@salesforce/resourceUrl/sounds_clap';
import Hihat from '@salesforce/resourceUrl/sounds_hihat';
import Kick from '@salesforce/resourceUrl/sounds_kick';
import Openhat from '@salesforce/resourceUrl/sounds_openhat'
import Ride from '@salesforce/resourceUrl/sounds_ride';
import Snare from '@salesforce/resourceUrl/sounds_snare';
import Tink from '@salesforce/resourceUrl/sounds_tink';
import Tom from '@salesforce/resourceUrl/sounds_tom'
export default class Drumkit extends LightningElement {
    wavMap = new Map();

    constructor() {
        this.wavMap.set(65, Clap);
        this.wavMap.set(83, Hihat);
        this.wavMap.set(68, Kick);
        this.wavMap.set(70, Openhat);
        this.wavMap.set(71, Boom);
        this.wavMap.set(72, Ride);
        this.wavMap.set(74, Snare);
        this.wavMap.set(75, Tom);
        this.wavMap.set(76, Tink)

    connectedCallback() {
        window.addEventListener('keypress', (event) => {

    disconnectedCallback() {
        window.removeEventListener("keypress", (event) => {
            this.playSound(event) })

    playSound(event) {
        const sound = new Audio(this.wavMap.get(event.keyCode));
        if (!sound){
        const element = this.template.querySelector(`[data-key="${event.keyCode}"]`);
        sound.currentTime = 0;

       if (event.propertyName !== 'transform'){

The Test

import {createElement} from 'lwc';
import Drumkit from 'c/drumkit'

describe('c-drumkit', ()=> {
    beforeEach(() => {
        const element = createElement('c-drumkit', {
            is: Drumkit
    test('attach event listener to window', ()=> {
        const drumkit = document.querySelector('c-drumkit');
        const element = drumkit.shadowRoot.querySelector('div[data-key="65"]');
       const keyPressEvent = new KeyboardEvent('keypress', {
           'keyCode': 65
       return Promise.resolve().then( () => {
           const element = drumkit.shadowRoot.querySelector('div[data-key="65"]');

1 Answer 1


With jest, you end up mocking modules/imports or apis that don't actually exist within the context of the test. That way, when your javascript interacts with them - a value is still returned when its called to let it go through its logic. For example, an apex method called by your lwc isn't actually called (as it isn't able to be) - but, you define a mock that returns a value you define when it's called.

In this case, you're interacting with HTMLMediaElement, but it does not exist in the context of a jest test. You'll have to mock the return/API so jest does not error and you can assert whether it was appropriately called or not.

You can leverage spyOn to mock it and keep track of it being called or not. Note that, within that method, it mentions that

By default, jest.spyOn also calls the spied method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use jest.spyOn(object, methodName).mockImplementation(() => customImplementation)

So, you also want to provide a mock implementation (value that is returned instead of actually calling it). In this case, it can just be empty so you can verify it's called.

cont playStub = jest
    .spyOn(window.HTMLMediaElement.prototype, 'play')
    .mockImplementation( () => {} );

       return Promise.resolve().then( () => {

The above confirms that the keypress does call HTMLMediaElement.play()

  • Thanks for the great answer I have not used the spyOn before. But I should have realized I was using a web API and not core JS. Nov 13, 2021 at 15:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .