Testing
Mock @mhpdev/react-native-speech in Jest tests with the provided mock configuration.
To mock the package's methods and components using the default mock configuration provided, follow these steps:
-
Create a file named
@mhpdev/react-native-speech.tsinside your__mocks__directory. -
Copy the following code into that file:
module.exports = require('@mhpdev/react-native-speech/jest');
What the mock provides
The mock returns a class-like default export and a HighlightedText mock, so your components render and your calls resolve without the native module:
| Member | Mock behavior |
|---|---|
Speech.maxInputLength | 4000 |
Speech.speak / speakWithOptions | Resolves undefined |
Speech.stop | Resolves undefined |
Speech.pause / resume / isSpeaking | Resolves false |
Speech.getAvailableVoices | Resolves [] |
Speech.getEngines | Resolves [] |
Speech.setEngine / openVoiceDataInstaller | Resolves undefined |
Speech.configure / reset | No-op |
Speech.onStart … Speech.onProgress | Return a subscription with a remove() spy |
HighlightedText | Renders null |
Because the methods are Jest mocks, you can assert against them in your tests:
import Speech from '@mhpdev/react-native-speech';
it('speaks the announced text', async () => {
await announce('Welcome');
expect(Speech.speak).toHaveBeenCalledWith('Welcome');
});The mock is installed through the __mocks__ file, so it applies to every
test file that imports the package. The event subscriptions expose Jest spies,
which you can also use to emit fake events if your tests need them.