Testing
Mock @mhpdev/react-native-haptics in Jest tests with the provided mock configuration.
To mock the package's methods using the default mock configuration provided, follow these steps:
-
Create a file named
@mhpdev/react-native-haptics.tsinside your__mocks__directory. -
Copy the following code into that file:
jest.mock('@mhpdev/react-native-haptics', () => require('@mhpdev/react-native-haptics/jest'), );
What the mock provides
The mock exposes all four methods as Jest mocks that resolve, so haptic calls in your components never touch the native module:
| Member | Mock behavior |
|---|---|
Haptics.impact | jest.fn() resolving |
Haptics.notification | jest.fn() resolving |
Haptics.selection | jest.fn() resolving |
Haptics.androidHaptics | jest.fn() resolving |
Because the methods are Jest mocks, you can assert against them in your tests:
import Haptics from '@mhpdev/react-native-haptics';
it('confirms the user action with haptics', () => {
confirmAction();
expect(Haptics.androidHaptics).toHaveBeenCalledWith('confirm');
});This mock uses jest.mock in the __mocks__ file, matching the library's
README. Unlike the automatic-mock pattern used by some packages, declare the
jest.mock(...) call exactly as shown above.