Testing with Jest
React Native Worklets is primarily a native library, for that reason it requires a proper setup to be tested with Jest. This guide will show you how to set it up when testing in a React Native project.
You have two options for this:
-
Enforce the use of mock implementation for React Native Worklets.
React Native Worklets provides a complete mock implementation for Jest. This is the recommended approach for testing.
-
Enforce the use of Web implementation for React Native Worklets.
React Native Worklets has a separate Web implementation to use in the browser. Use this approach when you want to avoid using artifical mocks.
Using the mock implementation
Depending on whether you are using TypeScript or JavaScript, apply the following mock:
- TypeScript
- JavaScript
jest.mock('react-native-worklets', () =>
require('react-native-worklets/src/mock')
);
jest.mock('react-native-worklets', () =>
require('react-native-worklets/lib/module/mock')
);
Using the Web implementation
Override Jest resolver to enforce resolving web implementation of React Native Worklets instead of pulling the native one. Modify your jest.config.js file to include the following code:
module.exports = {
// ... other configurations
resolver: 'react-native-worklets/jest/resolver',
};
Worklet modules in Jest
The Worklet Syntax plugin runs under Jest too. Without a Metro caller it emits each worklet in place, in the file that defines it, so no files are written and no extra setup is needed.
If you pass caller: { supportsInlineModules: true } to babel-jest, the plugin emits the modules inline instead, as it does under a Metro with inline module support. Use react-native-worklets/jest/resolver then: it writes those modules to a .jest-inline-modules directory under Jest's rootDir so that Jest can load them. Add that directory to your .gitignore.