I'm using Karma with Mocha, Chai and Sinon to test code in a project using this boilerplate. The Subject Under Test uses the Speech Synthesis API.
I start by establishing window.speechSynthesis.getVoices
in a beforeEach
method
beforeEach(() => {
global.window.speechSynthesis = {
getVoices: () => (null),
};
});
Then I have two test cases, in each one, I want to test what happens when a different set of voices is returned. To accomplish this I'm using Sinon stubs
First test case
it('supports speech and locale', () => {
const getVoicesStub = sinon.stub(
global.window.speechSynthesis,
'getVoices');
getVoicesStub.callsFake(() => (
[{lang: 'en_US'}]
));
Second test case
it('will choose best matching locale', () => {
const getVoicesStub = sinon.stub(
global.window.speechSynthesis,
'getVoices');
getVoicesStub.callsFake(() => (
[{lang: 'es_MX'}, {lang: 'es_US'}]
));
The problem is, when the SUT calls window.speechSynthesis.getVoices
during the second test case, it's getting the results from the first stub. It's as if the second stub is doing nothing...
If I comment out the first test case, the second test case succeeds, but if I leave them both in, the second one fails because the wrong set of voices are being returned.
Any idea how to get the second stub to work as expected?