测试失败:ReferenceError:未定义SpeechSynthesisUtterance

时间:2017-11-22 09:14:32

标签: javascript node.js reactjs typescript enzyme

我最近尝试在我的项目中实现Text to Speech,并决定使用简单的javascript解决方案。 (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);
    }
    /* */
}

一切正常,但每当我尝试测试代码时,我都会在标题中看到错误。

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

我在这个课程中测试的内容并不重要,我对这个组件的所有测试都失败了,因为Enzyme方法浅,mount和渲染不知道 SpeechSynthesisUtterance

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

我已经尝试过的事情:

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

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

  

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

我没有使用打字稿的经验,所以我的创意用完了下一步的尝试。

1 个答案:

答案 0 :(得分:0)

Typescript具有接口定义,因为该功能是浏览器中的全局 (检查chrome和firefox),因此您可以引用它。但是因为节点环境不是全局的,这就是你的测试失败的原因,你应该导入它进行测试,但是我不认为这是可用的,所以如果你想测试也许你可以为函数创建存根版本