The main problem problem with this was that it was really difficult to find out in which component or service this error was present. Also that sometimes test would run without any errors.
I did a global search for "play()" which was the root cause of the problem.
playAlertSound(){
const audio=new Audio()
audio.src= /* audio source link */
audio.load()
audio.play()
}
This is the code snippet which was causing the problem and specifically audio.play(). There is two approach we can take here:
- mock the above function call
- add below code snippet before audio.play() statement (not sure if this method is correct as we are altering the typescript file which can cause errors in the application)
audio.muted=true;
audio.autoplay=true;
I chose the first approach...the former being simple and easy.Doing something like this
spyOn(component,'playAlertSound').and.callFake(()=>{})
Mocking the function everytime it would be called.