Skip to content

fix(Reanimated): call lazy initializer in the useSharedValue mock - #10919

Merged
tjzel merged 2 commits into
mainfrom
@pawicao/mock-shared-value-lazy-init
Oct 26, 2026
Merged

tjzel merged 2 commits into
mainfrom
@pawicao/mock-shared-value-lazy-init

Conversation

@pawicao

@pawicao pawicao commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Note

This pull request was authored by AI on behalf of @pawicao.

Summary

The real useSharedValue accepts a function as the initial value and calls it once on mount since 4.4.0. The Jest mock did not call the function. It stored the function as the value, so useSharedValue(() => 5).get() returned the function. I changed the mock to call the initializer when init is a function.

Test plan

I added __tests__/mock.useSharedValue.test.tsx. Before the fix it failed with Expected: 5, Received: [Function anonymous]. After the fix it passes.

Changelog

  • I added a changelog fragment with yarn changelog:add for each changed package, or this PR does not change react-native-reanimated or react-native-worklets.

@pawicao
pawicao added this pull request to stack #10921 October 19, 2026 14:49
@pawicao
pawicao requested a review from tjzel October 19, 2026 14:53
@pawicao
pawicao marked this pull request as ready for review October 19, 2026 14:53
@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Caution

Review failed

The pull request is closed.

Walkthrough

The Jest mock of useSharedValue now accepts an initial value or a zero-argument initializer function. It calls the initializer to set the shared value’s initial value. A test checks that an initializer returning 5 produces a shared value whose get() returns 5. A changelog entry records the change.

Possibly related issues

Priority: Not assessed

Change: Bug fix

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: calling the lazy initializer in the Reanimated useSharedValue mock.
Description check Passed The description directly explains the mock bug, the fix, the added test, and the changelog update.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b41b1a04-abea-43a7-aa68-452dc38adcb9
📥 Commits

Reviewing files that changed from the base of the PR and between 1d2b213 and 146992e.

📒 Files selected for processing: 3
  • packages/react-native-reanimated/__tests__/mock.useSharedValue.test.tsx
  • packages/react-native-reanimated/changelog/mock-shared-value-lazy-init.fix.md
  • packages/react-native-reanimated/src/mock.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

Comment thread packages/react-native-reanimated/src/mock.ts
@tjzel
tjzel merged commit 2e54a76 into main Oct 26, 2026
24 of 25 checks passed
@tjzel
tjzel deleted the @pawicao/mock-shared-value-lazy-init branch October 26, 2026 12:45
tjzel added a commit that referenced this pull request Oct 26, 2026
…Value mock (#10920)

> [!NOTE]
> This pull request was authored by AI on behalf of @pawicao.

## Summary

Stacked on #10919.

The real `useSharedValue` keeps one shared value for the lifetime of the
component. The Jest mock created a new object on every call, so the
shared value changed identity on every render. Code that used it as a
`useEffect` or `useMemo` dependency ran again on every render under the
mock. I moved the mock shared value creation into the `useState`
initializer, so each component keeps one shared value.

## Test plan

I added a test to `__tests__/mock.useSharedValue.test.tsx` that
re-renders the hook and compares the results. Before the fix it failed,
after the fix it passes. The full reanimated Jest suite passes.

## Changelog

- [x] I added a changelog fragment with `yarn changelog:add` for each
changed package, or this PR does not change `react-native-reanimated` or
`react-native-worklets`.

Co-authored-by: Tomasz Żelawski <tzelawski@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants