testing失败:ReferenceError:SpeechSynthesisUtterance未定义

我最近试图在我的项目中实现文本到Speech,并决定使用纯javascript的解决scheme。 ( https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance )

我目前使用React@15.4.2和enzyme@2.9.1。

码:

export default class DummyComponent extends React.Component { /* */ speak(text) { var synth = window.speechSynthesis; var utterThis = new SpeechSynthesisUtterance(text); synth.speak(utterThis); } /* */ } 

一切工作正常,但每当我尝试testing我的代码,我得到的标题中的错误。

ExampleTest:

 it('dummytest', function(done){ const prop1 = 'foo'; const prop2 = 'bar'; const wrapper = shallow(<DummyComponent prop1 = {foo} prop2={bar}/>); expect(wrapper.html()).to.include("foobar"); done(); }); 

ReferenceError:SpeechSynthesisUtterance未定义

无论我在这个课堂上testing什么,我所有的这个组件的testing都失败了,因为酶的方法浅,安装和渲染不知道SpeechSynthesisUtterance

SpeechSynthesisUtterance实际上来自打字稿库。 (打字稿/ LIB / lib.es6.d.ts)

我已经尝试过的事情:

  • 将酶升级到3.2(产生完全相同的错误)
  • 导出function
  • 使用tsconfig文件来编译lib.es6.d.ts

对于后者,我尝试了一堆不同的设置,并在文件的顶部添加了一个“export {}”。 这实际上抛出了这个错误:

node_modules / typescript / lib / lib.es6.d.ts(5769,15):错误TS2470:符号引用不引用全局符号构造函数对象。

我没有打字稿的经验,所以我正在用尽想法下一步尝试。

Typescript具有接口定义,因为函数是浏览器中全局函数(请查看chrome和firefox),以便引用它。 但是,对于节点环境不是一个全球性的,这就是为什么你的testing失败,你应该导入它进行testing,但我不认为这是可用的,所以如果你想testing,也许你可以创build一个存根版本的function