Home > OS >  jest TypeError: undefined is not iterable
jest TypeError: undefined is not iterable

Time:04-05

I using jest for running my unit test but I am getting an error.

TypeError: Cannot read properties of undefined (reading 'getVideoTracks')

Any suggestions on how to test the below line in jest

[videoTrack] = (await navigator.mediaDevices.getUserMedia({video: true})).getVideoTracks();

My unit test file -

import { Track } from './Track';
import { } from '../index';

const mockMediaDevices = {
    getUserMedia: jest.fn()
};

Object.defineProperty(window.navigator, 'mediaDevices', {
    writable: true,
    value: mockMediaDevices,
});

Object.defineProperty(navigator.mediaDevices.getUserMedia, 'getVideoTracks', {
    writable: true,
    value: jest.fn(),
});

describe('Track', () => {
    describe('applyConstraints()', () => {
        let track: Track;
        let videoTrack: MediaStreamTrack;

        beforeEach(async () => {
            [videoTrack] = (await navigator.mediaDevices.getUserMedia({ video: true })).getVideoTracks();
            track = new Track(videoTrack as MediaStreamTrack);
        });

        it('should have applyConstraints method', () => {
            expect(typeof track.applyConstraints).toEqual('function');
        });
    });
});

Detailed Error -

TypeError: Cannot read properties of undefined (reading 'getVideoTracks')

      67 |
      68 |     beforeEach(async () => {
    > 69 |       [videoTrack] = (await navigator.mediaDevices.getUserMedia({video: true})).getVideoTracks();
         |                                                                                 ^
      70 |       track = new Track(videoTrack as MediaStreamTrack);
      71 |     });
      72 |

      at Object.<anonymous> (Media/Track/Track.test.ts:69:81)

I have tried this code -

const mockMediaDevices = {
  getUserMedia: jest.fn().mockReturnValue({ getVideoTracks: jest.fn() })
};
Object.defineProperty(navigator, 'mediaDevices', {
  writable: true,
  value: mockMediaDevices,
});

But this gives an error -

TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))

      73 |
      74 |     beforeEach(async () => {
    > 75 |       [videoTrack] = (await navigator.mediaDevices.getUserMedia({video: true})).getVideoTracks();

CodePudding user response:

try to change this code:

const mockMediaDevices = {
    getUserMedia: jest.fn()
};

Object.defineProperty(window.navigator, 'mediaDevices', {
    writable: true,
    value: mockMediaDevices,
});

Object.defineProperty(navigator.mediaDevices.getUserMedia, 'getVideoTracks', {
    writable: true,
    value: jest.fn(),
});

to (EDITED v2)

const mockMediaDevices = {
  getUserMedia: () => Promise.resolve({
    getVideoTracks: () => [jest.fn()],
  }),
};

Object.defineProperty(window.navigator, 'mediaDevices', {
  writable: true,
  value: mockMediaDevices,
});


beforeEach(async () => {
  const stream = await navigator.mediaDevices.getUserMedia({ video: true });
  [videoTrack] = stream.getVideoTracks();
  track = new Track(videoTrack as MediaStreamTrack);
});

because when you destruct your getVideoTracks() result you choose the element at 0 index. But jest.fn() is not an array that can be destructed as [videoTrack] = (await ....

  • Related