# corti

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

Latest version **2.1.0** (published 2026-03-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install corti
pnpm add corti
yarn add corti
bun add corti
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2026-03-07 |
| First published | 2015-12-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 68.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 24 |
| Author | Tal Ater |
| Maintainers | talater |
| Keywords | recognition, speech, speechrecognition, webkitSpeechRecognition, mock, test |

## Links

- npm: https://www.npmjs.com/package/corti
- Repository: https://github.com/TalAter/Corti
- Issues: https://github.com/TalAter/Corti/issues
- npm.io page: https://npm.io/package/corti

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2026-03-07
- 1.0.0-rc.1 (next) — 2024-06-11
- 2.0.0 — 2026-03-07
- 1.0.0 — 2024-06-11
- 0.2.0 — 2016-01-18
- 0.1.0 — 2015-12-24

## README

# Corti

Corti is a mock implementation of the browser’s SpeechRecognition API (aka webkitSpeechRecognition) for automated testing. It helps developers simulate speech recognition functionalities and test their applications in a controlled environment without relying on actual speech input.

💡 To easily use Speech Recognition in your own project, check out [annyang](https://github.com/TalAter/annyang).

## Features

- Mock SpeechRecognition API for testing purposes (can replace window.SpeechRecognition, window.webkitSpeechRecognition, window.mozSpeechRecognition, etc.)
- Compatible with all JavaScript testing frameworks
- Can be required (CJS), imported (ESM), or included via a script tag for testing in Node.js or in a browser
- Written in TypeScript — ships with full type declarations

- Adds additional functionality to the SpeechRecognition API to support programmatically simulating speech and checking the current status

## Getting Started

### Installation

Install `corti` as a dev dependency using npm / pnpm / bun / yarn / etc:

```sh
npm install --save-dev corti
```

### Sample Usage

#### 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 (script tag)

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/corti/2.0.0/corti.iife.min.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 [how SpeechRecognition is mocked and tested in Corti](https://github.com/TalAter/Corti/tree/master/test/specs).

### 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()`

### Types Exported

- `CortiSpeechRecognition` — Extends the DOM's `SpeechRecognition` interface with `say()` and `isStarted()`

### Author

Tal Ater: [@TalAter](https://twitter.com/TalAter)

### License

Licensed under [MIT](https://github.com/TalAter/Corti/blob/master/LICENSE).

---
_Source: https://npm.io/package/corti · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
