
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Corti 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:
npm install --save-dev corti
// 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?');
});
});
<script type="module">
// Mock native SpeechRecognition
import { SpeechRecognition } from 'corti.js';
window.SpeechRecognition = SpeechRecognition;
// Run some tests
const recognition = new window.SpeechRecognition();
recognition.onresult = () => console.log('I hear it!');
recognition.start();
recognition.say('Hello world');
</script>
<script src="../dist/corti.js"></script>
<script>
// Mock native SpeechRecognition
window.SpeechRecognition = corti.SpeechRecognition;
// Run some tests
const recognition = new window.SpeechRecognition();
recognition.onresult = () => console.log('I hear it!');
recognition.start();
recognition.say('Hello world');
</script>
For an example of how Corti is used in a real project, check out SpeechKITT.
start()
abort()
stop()
addEventListener()
interimResults
lang
continuous
maxAlternatives
onstart
onend
onresult
onsoundstart
start
end
result
soundstart
SpeechRecognition
SpeechRecognitionEvent
SpeechRecognitionResultList
SpeechRecognitionResult
SpeechRecognitionAlternative
isStarted()
say()
Tal Ater: @TalAter
Licensed under MIT.
FAQs
Replace window.SpeechRecognition with a mock object and automate your tests
The npm package corti receives a total of 58 weekly downloads. As such, corti popularity was classified as not popular.
We found that corti demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.