简体   繁体   English

用Jest和Enzyme测试DashJS

[英]Testing DashJS with Jest & Enzyme

I'm trying to write Jest tests for a React component which contains a DashJS media player. 我正在尝试为包含DashJS媒体播放器的React组件编写Jest测试。 I'm using Enzyme's mount method to try and test the component, but it seems that the DashJS media player fails to mount properly. 我正在使用Enzyme的mount方法来尝试测试组件,但似乎DashJS媒体播放器无法正确安装。

In my componentDidMount method, I have the following code: 在我的componentDidMount方法中,我有以下代码:

    this.videoManager = dashjs.MediaPlayer().create();
    this.videoManager.initialize(this.videoPlayer, videoUrl, true);
    // Where this.videoPlayer is a reference to an HTML <video> element
    this.videoManager.preload();

The last line ( this.videoManager.preload(); ) produces the following error: 最后一行( this.videoManager.preload(); )产生以下错误:

You must first call attachSource() with a valid source before calling this method thrown 在调用抛出此方法之前,必须首先使用有效源调用attachSource()

When I run the component it works normally - it's only the testing I'm having issues with. 当我运行组件时,它正常工作 - 这只是我遇到问题的测试。 I haven't been able to find any related issues/solutions online. 我无法在线找到任何相关问题/解决方案。

I'm using the following versions of each relevant package: 我正在使用每个相关包的以下版本:

  • react: "16.2.0"
  • dashjs: "2.6.7"
  • jest: "22.3.0"
  • enzyme: "3.3.0"
  • enzyme-adapter-react-16: "1.1.1"

Any help will be appreciated! 任何帮助将不胜感激!

That error implies that there was some issue with videoUrl , which caused the value passed in initialize not to be set. 该错误意味着videoUrl存在一些问题,导致initialize传递的值不被设置。 When preload checks that a valid source has been set, the error is thrown. preload检查已设置有效源时,将引发错误。

At a guess, is videoUrl an empty string in your test but non-zero length when the component is used normally? 猜测,在测试中, videoUrl是一个空字符串,但正常使用该组件时是非零长度吗?

Looking at this again, the problem is probably that you are (presumably) using JSDOM to provide the DOM for your tests, and JSDOM does not provide MediaSource or WebKitMediaSource in window . 再看一遍,问题可能是你(大概)使用JSDOM为你的测试提供DOM,而JSDOM不在window提供MediaSourceWebKitMediaSource This causes dash.js to fail to initialise. 这会导致dash.js无法初始化。 dash.js should throw a capability error which can be caught using player.on('error', () => {}) . dash.js应抛出一个能力错误,可以使用player.on('error', () => {})捕获。

As a side note, you are providing a video object to initialize , as well as setting autoplay to true . 作为旁注,您提供了一个视频对象来initialize ,以及将自动播放设置为true Doing the first will cause preload do nothing since it will just load segments in to the SourceBuffer instead, which probably isn't what you wanted. 做第一个会导致preload什么都不做,因为它只是将段加载到SourceBuffer中,这可能不是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM