Comparing version 0.2.0 to 1.0.0-rc.1
{ | ||
"name": "corti", | ||
"version": "0.2.0", | ||
"version": "1.0.0-rc.1", | ||
"description": "Replace window.SpeechRecognition with a mock object and automate your tests", | ||
"main": "corti.js", | ||
"scripts": { | ||
"test": "grunt test" | ||
"keywords": ["recognition", "speech", "speechrecognition", "webkitSpeechRecognition"], | ||
"homepage": "https://github.com/TalAter/Corti", | ||
"bugs": { | ||
"url": "https://github.com/TalAter/Corti/issues" | ||
}, | ||
"license": "MIT", | ||
"author": "Tal Ater <tal@talater.com> (https://www.talater.com/)", | ||
"main": "dist/corti.cjs.js", | ||
"module": "dist/corti.mjs", | ||
"browser": "dist/corti.js", | ||
"exports": { | ||
".": { | ||
"import": "./dist/corti.mjs", | ||
"require": "./dist/corti.cjs.js", | ||
"default": "./dist/corti.js" | ||
} | ||
}, | ||
"repository": { | ||
@@ -13,21 +26,26 @@ "type": "git", | ||
}, | ||
"keywords": [ | ||
"speechrecognition", | ||
"webkitSpeechRecognition", | ||
"speech", | ||
"recognition" | ||
], | ||
"author": "Tal Ater <tal@talater.com> (https://www.talater.com/)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/TalAter/Corti/issues" | ||
"scripts": { | ||
"build": "rollup -c", | ||
"build:watch": "rollup -c -w", | ||
"dev": "npm-run-all --parallel format:watch lint:watch build:watch", | ||
"format": "prettier --write 'src/**/*.js' 'test/**/*.js' 'test/**/*.html' 'package.json' '.eslintrc.json'", | ||
"format:watch": "chokidar 'src/**/*.js' 'test/**/*.js' 'test/**/*.html' 'package.json' '.eslintrc.json' -c 'npm run format'", | ||
"lint": "eslint 'src/**/*.js' 'test/**/*.js'", | ||
"lint:watch": "chokidar 'src/**/*.js' 'test/**/*.js' -c 'npm run lint'", | ||
"test": "vitest" | ||
}, | ||
"homepage": "https://github.com/TalAter/Corti", | ||
"devDependencies": { | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-jasmine": "^0.9.2", | ||
"grunt-contrib-jshint": "^0.11.3", | ||
"grunt-contrib-watch": "^0.6.1", | ||
"grunt-template-jasmine-istanbul": "^0.4.0" | ||
} | ||
"chokidar-cli": "^3.0.0", | ||
"eslint": "^8.57.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-plugin-import": "^2.29.1", | ||
"eslint-plugin-prettier": "^5.1.3", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^3.3.1", | ||
"rollup": "^4.18.0", | ||
"vitest": "^1.6.0" | ||
}, | ||
"files": ["dist", "LICENSE", "README.md"], | ||
"packageManager": "pnpm@9.1.2+sha512.127dc83b9ea10c32be65d22a8efb4a65fb952e8fefbdfded39bdc3c97efc32d31b48b00420df2c1187ace28c921c902f0cb5a134a4d032b8b5295cbfa2c681e2" | ||
} |
157
README.md
# Corti | ||
[![Build Status](https://travis-ci.org/TalAter/Corti.svg?branch=master)](https://travis-ci.org/TalAter/Corti) [![Dependency Status](https://gemnasium.com/TalAter/Corti.svg)](https://gemnasium.com/TalAter/Corti) | ||
Corti is a drop in replacement for the browser's SpeechRecognition object. It mocks some of the behaviour of the native object to facilitate automated testing, and provides a number of extra methods beyond the spec to help testing. | ||
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). | ||
For an example of using Corti to test a real project, check out [SpeechKITT](https://github.com/TalAter/SpeechKITT). | ||
💡 To easily use Speech Recognition in your own project, check out [annyang](https://github.com/TalAter/annyang). | ||
To easily use Speech Recognition in your own project, check out [annyang](https://github.com/TalAter/annyang). | ||
## Getting Started | ||
### Sample Test With Corti | ||
### Installation | ||
````javascript | ||
// Patch the current environment with a mock Speech Recognition object | ||
Corti.patch(); | ||
Install `corti` as a dev dependency using npm: | ||
// Interact with the mock object, like you would with the real SpeechRecognition object | ||
var recognition = new window.SpeechRecognition(); | ||
recognition.onstart = function() {console.log("I'm listening");}; | ||
recognition.addEventListener('result', function(sre) { | ||
console.log(sre.results.item(sre.resultIndex).item(0).transcript); | ||
```bash | ||
npm install --save-dev corti | ||
``` | ||
### Usage | ||
#### In node.js | ||
```javascript | ||
// Vitest example | ||
import { SpeechRecognition } from 'corti'; | ||
import { describe, it, expect, beforeEach, beforeAll, afterAll, vi } from 'vitest'; | ||
beforeAll(() => { | ||
vi.stubGlobal('SpeechRecognition', SpeechRecognition); | ||
}); | ||
recognition.addEventListener('end', function() {console.log("Quiet");}); | ||
recognition.continuous = true; | ||
// Use extra utility methods added to the mock object to assist with testing | ||
expect(recognition.isStarted()).toBe(false); | ||
recognition.start(); | ||
expect(recognition.isStarted()).toBe(true); | ||
recognition.abort(); | ||
expect(recognition.isStarted()).toBe(false); | ||
afterAll(() => { | ||
vi.unstubAllGlobals(); | ||
}); | ||
// Simulate speech recognition | ||
recognition.addEventListener('result', mySpyFunction); | ||
expect(mySpyFunction).not.toHaveBeenCalled(); | ||
recognition.say("Next time you want to stab me in the back, have the guts to do it to my face"); | ||
expect(mySpyFunction).toHaveBeenCalled(); | ||
```` | ||
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 Browser (ESM) | ||
```html | ||
<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) | ||
```html | ||
<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](https://github.com/TalAter/SpeechKITT). | ||
### Methods Mocked | ||
* start() | ||
* abort() | ||
* stop() | ||
* addEventListener() | ||
* `start()` | ||
* `abort()` | ||
* `stop()` | ||
* `addEventListener()` | ||
### Attributes Mocked | ||
* interimResults | ||
* lang | ||
* continuous | ||
* maxAlternatives | ||
* onstart | ||
* onend | ||
* onresult | ||
* `interimResults` | ||
* `lang` | ||
* `continuous` | ||
* `maxAlternatives` | ||
* `onstart` | ||
* `onend` | ||
* `onresult` | ||
* `onsoundstart` | ||
### Events Mocked | ||
* start | ||
* end | ||
* result | ||
* `start` | ||
* `end` | ||
* `result` | ||
* `soundstart` | ||
### Event Objects Mocked | ||
### Objects Mocked | ||
* SpeechRecognitionEvent | ||
* SpeechRecognitionResultList | ||
* SpeechRecognitionResult | ||
* SpeechRecognitionAlternative | ||
* `SpeechRecognition` | ||
* `SpeechRecognitionEvent` | ||
* `SpeechRecognitionResultList` | ||
* `SpeechRecognitionResult` | ||
* `SpeechRecognitionAlternative` | ||
### Extra Utility Methods Added To Object | ||
### Extra Utility Methods Added To Mocked SpeechRecognition Object | ||
* isStarted() | ||
* say() | ||
* `isStarted()` | ||
* `say()` | ||
@@ -74,0 +133,0 @@ ### Author |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
89439
138
10
9
904