I am writing a small library at the moment to help with connecting to media devices, it would be nice if I could unit test the library, I have a function in my typescript library that looks like this,
static connectAudioDevice(device?: InputDeviceInfo) : Promise<MediaStream> {
return new Promise<MediaStream>((resolve, reject) => {
navigator.mediaDevices.getUserMedia({audio: true})
.then(stream => {
resolve(stream);
})
.catch(error => {
resolve(error);
})
})
}
I want to use jest.spyOn
to mock the getUserMedia
is this possible? Something like,
import devices from '@/assets/lib/devices.ts';
test('Resolves with valid audio mediaStream', () => {
const audioStream = {};
let promise = devices.connectAudioDevice();
jest.spyOn(window.navigator.mediaDevices, 'getUserMedia', promise);
})
But I am getting the error:
Cannot spyOn on a primitive value; undefined given
Can anyone offer me some help?