0

I'm writing a React app in which I use AudioContext. Sometimes on certain versions of Safari, calling new with AudioContext (or window.AudioContext || window.webkitAudioContext;) returns null.

I've written code that guards against this. I was wondering if there is a way to imitate this behaviour? I would like to create a unit test in which I mock window.AudioContext to be a class that when called with new returns null.

How can you achieve something so that:

const context = new FakeAudioContext();

sets context to null?

J. Hesters
  • 13,117
  • 31
  • 133
  • 249
  • This feature is [not fully supported yet on safari](https://caniuse.com/mdn-api_audiocontext_audiocontext) – Nicolas Nov 20 '20 at 16:35
  • I don't think it's possible unless you can change the syntax of your call to remove the `new` (or can accept invalid values that aren't `null`) – CertainPerformance Nov 20 '20 at 16:36
  • @Amy Thanks, I saw that! No it does not. The solutions described there do not work. – J. Hesters Nov 20 '20 at 16:38
  • 1
    @J.Hesters That's because you can't, and the answers there are the best you can do. –  Nov 20 '20 at 16:41

1 Answers1

-1

you can simply do it like this for the sake of your unit test window.AudioContext = null;

and that way you will simulating the behavior when it's null

  • Unfortunately no, because the code calls `new window.AudioContext()` which is different, because it doesn't return `null` but it crashes saying that `null` is not a constructor. – J. Hesters Nov 20 '20 at 16:39