Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Replace window.SpeechRecognition with a mock object and automate your tests
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?');
});
});
// 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);
});
<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
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.