Skip to content

Commit 0e1b557

Browse files
committed
feat: Add worklet mock helper function for jest unit tests (#8882)
## Summary This PR adds a helper `worklet` function to create worklet function mocks (functions that pretend to be worklets instead of plain functions). Sometimes behavior of our code is dependent on whether a function is a worklet or not - to distinguish this behavior in tests, I need to have a way to create function mocks that will be treated as worklets.
1 parent 702ab6b commit 0e1b557

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
import type { WorkletFunction } from 'react-native-worklets';
4+
5+
/**
6+
* Converts any callback function to a mock worklet function for testing
7+
* purposes. This function simulates a worklet by adding the required internal
8+
* properties.
9+
*
10+
* @param callback - Optional callback function to wrap as a worklet. If not
11+
* provided, returns an empty worklet.
12+
* @returns A mock worklet function with the required worklet properties.
13+
*/
14+
export const worklet = <Args extends unknown[] = [], ReturnValue = void>(
15+
callback?: (...args: Args) => ReturnValue
16+
): WorkletFunction<Args, ReturnValue> => {
17+
const fn = (callback ?? (() => undefined)) as WorkletFunction<
18+
Args,
19+
ReturnValue
20+
>;
21+
fn.__workletHash = Math.random();
22+
fn.__closure = {};
23+
return fn;
24+
};

packages/react-native-reanimated/src/jestUtils.ts renamed to packages/react-native-reanimated/src/jestUtils/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/* eslint-disable @typescript-eslint/no-namespace */
22
'use strict';
33

4+
import type React from 'react';
45
import type { ReactTestInstance } from 'react-test-renderer';
56

6-
import { IS_JEST, logger, ReanimatedError } from './common';
7+
import { IS_JEST, logger, ReanimatedError } from '../common';
78
import type {
89
AnimatedComponentProps,
910
AnimatedProps,
1011
IAnimatedComponentInternal,
1112
InitialComponentProps,
12-
} from './createAnimatedComponent/commonTypes';
13-
import type { DefaultStyle } from './hook/commonTypes';
13+
} from '../createAnimatedComponent/commonTypes';
14+
import type { DefaultStyle } from '../hook/commonTypes';
1415

1516
declare global {
1617
namespace jest {
@@ -353,3 +354,6 @@ export const getAnimatedStyle = (component: ReactTestInstance) => {
353354
component as unknown as TestComponent
354355
);
355356
};
357+
358+
/** @knipIgnore */
359+
export { worklet } from './common';

packages/react-native-reanimated/src/jestUtils.web.ts renamed to packages/react-native-reanimated/src/jestUtils/index.web.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ export function setUpTests() {
2222
export function getAnimatedStyle() {
2323
// NOOP
2424
}
25+
26+
export { worklet } from './common';

0 commit comments

Comments
 (0)