Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

corti

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

corti

Replace window.SpeechRecognition with a mock object and automate your tests

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
92
increased by26.03%
Maintainers
1
Weekly downloads
 
Created
Source

Corti

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.

Getting Started

Installation

Install corti as a dev dependency using npm:

npm install --save-dev corti

Usage

In node.js (ESM)
// 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)
// 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)
<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>
In Browser (without modules)
<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.

Methods Mocked

  • start()
  • abort()
  • stop()
  • addEventListener()

Attributes Mocked

  • interimResults
  • lang
  • continuous
  • maxAlternatives
  • onstart
  • onend
  • onresult
  • onsoundstart

Events Mocked

  • start
  • end
  • result
  • soundstart

Objects Mocked

  • SpeechRecognition
  • SpeechRecognitionEvent
  • SpeechRecognitionResultList
  • SpeechRecognitionResult
  • SpeechRecognitionAlternative

Extra Utility Methods Added To Mocked SpeechRecognition Object

  • isStarted()
  • say()

Author

Tal Ater: @TalAter

License

Licensed under MIT.

Keywords

FAQs

Package last updated on 11 Jun 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc