1
votes

I recently tried to implement text to Speech in my project and decided to go with the plain javascript solution. (https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance)

I am currently using [email protected] and [email protected].

Code:

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

Everything works fine, but whenever I try to test my code I get the error in the title.

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 is not defined

It does not matter what I test in this class, all my tests for this component are failing, due to the fact that the Enzyme methods shallow, mount and render do not know SpeechSynthesisUtterance.

SpeechSynthesisUtterance actually comes from the typescript lib. (typescript/lib/lib.es6.d.ts)

Things I already tried:

  • Upgrading Enzyme to 3.2 (yields in exactly the same error)
  • exporting the function
  • using a tsconfig file to compile the lib.es6.d.ts

For the latter I tried a bunch of different settings and experimented with adding an "export {}" at the top of the file. This actually threw this error:

node_modules/typescript/lib/lib.es6.d.ts(5769,15): error TS2470: 'Symbol' reference does not refer to the global Symbol constructor object.

I have no experience with typescript, so I am running out of Ideas what to try next.

1

1 Answers

0
votes

Typescript has the interface definition because the function is a global in browsers(check chrome and firefox) so you could just reference it. But for the node environment is not a global and thats why your test is failing, you should import it for test it, but i don't think that is available so if you want to test maybe you could create a stub version for the function