cookpete / react-player

A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia and DailyMotion
https://cookpete.github.io/react-player
MIT License
9.03k stars 1.13k forks source link

Segmentation Fault when rendering 'react-player/lazy' with Jest & React Testing Library #1391

Open pjaws opened 2 years ago

pjaws commented 2 years ago

Be sure to search for your issue before opening a new one.

Current Behavior

Importing ReactPlayer from react-player/lazy causes a segmentation fault when rendered with @testing-library/react inside a Jest test.

Importing from react-player/vimeo does not produce the same issue.

Expected Behavior

:)

Steps to Reproduce

video.js

import PropTypes from 'prop-types';
// switching this to 'react-player/vimeo' will work
import ReactPlayer from 'react-player/lazy';

import { track } from 'utils/analytics';

const Video = ({ title, url, width, height }) => {
  const handleStart = () =>
    track('video_started', {
      video_title: title,
      video_url: url,
    });

  const handlePause = () =>
    track('video_paused', {
      video_title: title,
      video_url: url,
    });

  const handlePlay = () =>
    track('video_played', {
      video_title: title,
      video_url: url,
    });

  const handleProgress = ({ played, playedSeconds }) =>
    track('video_progress', {
      video_title: title,
      video_url: url,
      video_played: played,
      video_played_seconds: playedSeconds,
    });

  const handleEnded = () =>
    track('video_ended', {
      video_title: title,
      video_url: url,
    });

  return (
    <ReactPlayer
      className="react-player"
      url={url}
      config={{ vimeo: { title } }}
      onStart={handleStart}
      onPause={handlePause}
      onPlay={handlePlay}
      onProgress={handleProgress}
      onEnded={handleEnded}
      width={width}
      height={height}
      controls
    />
  );
};

export default Video;

video.spec.js

import { render } from '@testing-library/react';

import Video from 'components/video';

describe('Video', () => {
  it('matches the snapshot', () => {
    const { container } = render(
      <Video
        url="https://player.vimeo.com/video/457872455"
        title="example"
        width={970}
        height={546}
      />
    );

    expect(container).toMatchSnapshot();
  });
});

results in:

 RUNS  src/__tests__/pages/video-example.spec.js
[1]    2860 segmentation fault  node --expose-gc --trace-warnings ./node_modules/.bin/jest video-example

Environment

seisenreich commented 2 years ago

Same problem here. As soon as I use "react-player/lazy" instead of "react-player/youtube" or "react-player" jest tests using react-testing-library fail with segmentation fault.

babipal commented 2 years ago

Same here.

yashwant-dangi commented 2 years ago

Is anyone able to find the solution to this?

F3n67u commented 1 year ago

Same here.

My solution is to replace react-player/lazy module with react-player in your jest setup file:

// Replace react-player/lazy with react-player
// Fix Segmentation Fault when using 'react-player/lazy' in tests
// Learn more: https://github.com/cookpete/react-player/issues/1391
jest.mock('react-player/lazy', () => {
  return jest.requireActual('react-player');
});
AgustinJuarezN commented 1 year ago

Having the same issue. I fixed it for now with @F3n67u solution but I was wondering if it will be fixed officially.