Replace window.SpeechRecognition with a mock object and automate your tests
npm install cortiCorti is a drop in replacement for the browser's SpeechRecognition object. It mocks the behaviour of the native object to facilitate automated testing, and provides a number of extra methods beyond the SpeechRecognition spec to help testing (e.g., to simulate speech in automated tests).
š” To easily use Speech Recognition in your own project, check out annyang.
Install corti as a dev dependency using npm:
``bash`
npm install --save-dev corti
#### In node.js (ESM)
`javascript
// Vitest example
import { SpeechRecognition } from 'corti';
import { describe, it, expect, beforeEach, beforeAll, afterAll, vi } from 'vitest';
beforeAll(() => {
vi.stubGlobal('SpeechRecognition', SpeechRecognition);
});
afterAll(() => {
vi.unstubAllGlobals();
});
describe('Mirror mirror on the wall', () => {
let recognition;
let spyFn;
beforeEach(() => {
recognition = new globalThis.SpeechRecognition();
spyFn = vi.fn();
recognition.maxAlternatives = 5;
recognition.onresult = spyFn;
recognition.start();
});
it('should call callback when called with a single sentence', () => {
recognition.say('Hello world');
expect(spyFn).toHaveBeenCalled();
const event = spyFn.mock.calls[0][0];
expect(event.results[0][0].transcript).toBe('Hello world');
});
it('should call callback when called with multiple sentences', () => {
recognition.say(['Hello world', 'How are you?']);
expect(spyFn).toHaveBeenCalled();
const event = spyFn.mock.calls[0][0];
expect(event.results[0][0].transcript).toBe('Hello world');
expect(event.results[0][1].transcript).toBe('How are you?');
});
});
`
#### In node.js (CJS)
`javascript
// Jest example
const { SpeechRecognition } = require('corti');
beforeAll(() => {
global.SpeechRecognition = SpeechRecognition;
});
test('SpeechRecognition', () => {
const speech = new globalThis.SpeechRecognition();
const spyFn = jest.fn();
speech.onresult = spyFn;
speech.continuous = true;
speech.start();
speech.say('Hello world');
speech.say('Hello world');
expect(spyFn.mock.calls.length).toBe(2);
});
`
#### In Browser (ESM)
`html`
#### In Browser (without modules)
`html`
For an example of how Corti is used in a real project, check out SpeechKITT.
* start()abort()
* stop()
* addEventListener()
*
* interimResultslang
* continuous
* maxAlternatives
* onstart
* onend
* onresult
* onsoundstart
*
* startend
* result
* soundstart
*
* SpeechRecognitionSpeechRecognitionEvent
* SpeechRecognitionResultList
* SpeechRecognitionResult
* SpeechRecognitionAlternative
*
* isStarted()say()`
*