react-native-reanimated: Snapshots excessively large or failing with `Invalid string length`

Description

Since Reanimated v3.16.0, snapshots using @testing-library/react-native have been breaking. In some cases, the snapshot size has increased drastically, going from less than 1 KB to over 250 MB. In other cases, snapshots fail to generate, throwing an Invalid string length error.

v3.15.5

 PASS  ./ComponentTwo.test.tsx
 › 1 snapshot written.
 PASS  ./ComponentOne.test.tsx
 › 1 snapshot written.

Snapshot Summary
 › 2 snapshots written from 2 test suites.

Test Suites: 2 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   2 written, 2 total
Time:        1.938 s, estimated 22 s
➜ ls -lh
total 16
-rw-r--r--@ 1 brianvalente  staff   586B Oct 28 16:22 ComponentOne.test.tsx.snap
-rw-r--r--@ 1 brianvalente  staff   925B Oct 28 16:22 ComponentTwo.test.tsx.snap

v3.16.1

 PASS  ./ComponentOne.test.tsx (6.267 s)
 › 1 snapshot written.
 FAIL  ./ComponentTwo.test.tsx (9.18 s)
  ● ComponentTwo › should pass

    RangeError: Invalid string length

       6 |     it("should pass", () => {
       7 |         const rendered = render(<ComponentTwo />).toJSON();
    >  8 |         expect(rendered).toMatchSnapshot();
         |                          ^
       9 |     });
      10 | })
      11 |

      at printElement (node_modules/jest-snapshot/node_modules/pretty-format/build/plugins/lib/markup.js:105:36)
      at Object.toMatchSnapshot (ComponentTwo.test.tsx:8:26)

Snapshot Summary
 › 1 snapshot written from 1 test suite.

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   1 written, 1 total
Time:        9.404 s
➜ ls -lh
total 537280
-rw-r--r--@ 1 brianvalente  staff   262M Oct 28 16:23 ComponentOne.test.tsx.snap

Steps to reproduce

  1. Clone demo repo
  2. Install dependencies
  3. Run npm run test

Snack or a link to a repository

https://github.com/BrianValente/reanimated-jest-snapshot-issue-demo

Reanimated version

3.6.1

React Native version

0.74.5

Platforms

Android, iOS, macOS, Web

JavaScript runtime

Hermes

Workflow

Expo Dev Client

Architecture

Paper (Old Architecture)

Build type

Other (please specify)

Device

None

Device model

No response

Acknowledgements

Yes

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 12
  • Comments: 21

Commits related to this issue

Most upvoted comments

An alternative patch that worked here:

diff --git a/node_modules/react-native-reanimated/lib/module/createAnimatedComponent/createAnimatedComponent.js b/node_modules/react-native-reanimated/lib/module/createAnimatedComponent/createAnimatedComponent.js
index 69682cb..229b26c 100644
--- a/node_modules/react-native-reanimated/lib/module/createAnimatedComponent/createAnimatedComponent.js
+++ b/node_modules/react-native-reanimated/lib/module/createAnimatedComponent/createAnimatedComponent.js
@@ -392,8 +392,8 @@ export function createAnimatedComponent(Component, options) {
       const skipEntering = this.context?.current;
       const nativeID = skipEntering || !isFabric() ? undefined : `${this.reanimatedID}`;
       const jestProps = IS_JEST ? {
-        jestInlineStyle: this.props.style,
-        jestAnimatedStyle: this.jestAnimatedStyle
+        // jestInlineStyle: this.props.style,
+        // jestAnimatedStyle: this.jestAnimatedStyle
       } : {};
       return /*#__PURE__*/React.createElement(Component, _extends({
         nativeID: nativeID

Applying @Grohden 's patch works! However, I found that tests that use expect(component).toHaveAnimatedStyle(...) would fail. Here’s an updated patch that fixes this

diff --git a/lib/module/createAnimatedComponent/createAnimatedComponent.js b/lib/module/createAnimatedComponent/createAnimatedComponent.js
index 69682cb3b85d04d83ae29dc8d07e16204e62b29c..62fe52439729a7280c1fc5d33addc53fd367a5ca 100644
--- a/lib/module/createAnimatedComponent/createAnimatedComponent.js
+++ b/lib/module/createAnimatedComponent/createAnimatedComponent.js
@@ -172,9 +172,9 @@ export function createAnimatedComponent(Component, options) {
       } else {
         const hostInstance = findHostInstance(this);
         if (!hostInstance) {
-          /* 
-            findHostInstance can return null for a component that doesn't render anything 
-            (render function returns null). Example: 
+          /*
+            findHostInstance can return null for a component that doesn't render anything
+            (render function returns null). Example:
             svg Stop: https://github.com/react-native-svg/react-native-svg/blob/develop/src/elements/Stop.tsx
           */
           throw new ReanimatedError('Cannot find host instance for this component. Maybe it renders nothing?');
@@ -392,8 +392,10 @@ export function createAnimatedComponent(Component, options) {
       const skipEntering = this.context?.current;
       const nativeID = skipEntering || !isFabric() ? undefined : `${this.reanimatedID}`;
       const jestProps = IS_JEST ? {
-        jestInlineStyle: this.props.style,
-        jestAnimatedStyle: this.jestAnimatedStyle
+        // In react-native-reanimated 3.16.0>, there's a bug that generates components infinitely, because of the circular reference in jestAnimatedStyle.
+        // To fix this, let's temporarily not pass the jestAnimatedStyle to the component.
+        // jestInlineStyle: this.props.style,
+        // jestAnimatedStyle: this.jestAnimatedStyle
       } : {};
       return /*#__PURE__*/React.createElement(Component, _extends({
         nativeID: nativeID
diff --git a/lib/module/jestUtils.js b/lib/module/jestUtils.js
index 09310c12af916c38bb51f6297a08fc880c1ede67..d41a4a07978ea9ce18d88a5bcad9dd8b03d65b22 100644
--- a/lib/module/jestUtils.js
+++ b/lib/module/jestUtils.js
@@ -6,7 +6,7 @@ import { ReanimatedError } from "./errors.js";
 const defaultFramerateConfig = {
   fps: 60
 };
-const isEmpty = obj => Object.keys(obj).length === 0;
+const isEmpty = (obj = {}) => Object.keys(obj).length === 0;
 const getStylesFromObject = obj => {
   return obj === undefined ? {} : Object.fromEntries(Object.entries(obj).map(([property, value]) => [property, value._isReanimatedSharedValue ? value.value : value]));
 };

Here is happening with reanimated version 3.16.x, while on 3.15.x is not happening

Hey @ilteoood,

Same here. This unblocks me, and now I will be able to upgrade to newest expo-sdk52

Thanks!

Here is happening with reanimated version 3.16.x, while on 3.15.x is not happening

Update: unfortunately I have Android build errors on version 3.15, so stuck again 😦

> Task :react-native-reanimated:compileDebugJavaWithJavac FAILED
/Users/rotoxl/dev/beone-mobile/node_modules/react-native-reanimated/android/src/main/java/com/swmansion/reanimated/NodesManager.java:456: warning: [removal] ReactViewBackgroundDrawable in com.facebook.react.views.view has been deprecated and marked for removal
        if (!(background instanceof ReactViewBackgroundDrawable)) {
                                    ^
/Users/rotoxl/dev/beone-mobile/node_modules/react-native-reanimated/android/src/main/java/com/swmansion/reanimated/NodesManager.java:459: warning: [removal] ReactViewBackgroundDrawable in com.facebook.react.views.view has been deprecated and marked for removal
        int actualColor = ((ReactViewBackgroundDrawable) background).getColor();
                            ^
/Users/rotoxl/dev/beone-mobile/node_modules/react-native-reanimated/android/src/main/java/com/swmansion/reanimated/layoutReanimation/ReanimatedNativeHierarchyManager.java:285: error: no suitable method found for updateLayout(int,int,int,int,int,int)
    super.updateLayout(parentTag, tag, x, y, width, height);
         ^
    method NativeViewHierarchyManager.updateLayout(int,int,int,int,int) is not applicable
      (actual and formal argument lists differ in length)
    method NativeViewHierarchyManager.updateLayout(int,int,int,int,int,int,YogaDirection) is not applicable
      (actual and formal argument lists differ in length)
    method NativeViewHierarchyManager.updateLayout(View,int,int,int,int) is not applicable
      (actual and formal argument lists differ in length)
/Users/rotoxl/dev/beone-mobile/node_modules/react-native-reanimated/android/src/reactNativeVersionPatch/BorderRadiiDrawableUtils/latest/com/swmansion/reanimated/BorderRadiiDrawableUtils.java:15: error: incompatible types: bad type in conditional expression
      float full = uniform != null ? uniform.resolve(view.getWidth(), view.getHeight()) : Float.NaN;
                                                    ^
    CornerRadii cannot be converted to float
/Users/rotoxl/dev/beone-mobile/node_modules/react-native-reanimated/android/src/reactNativeVersionPatch/BorderRadiiDrawableUtils/latest/com/swmansion/reanimated/BorderRadiiDrawableUtils.java:16: warning: [removal] getComputedBorderRadius() in CSSBackgroundDrawable has been deprecated and marked for removal
      ComputedBorderRadius computedBorderRadius = drawable.getComputedBorderRadius();
                                                          ^
/Users/rotoxl/dev/beone-mobile/node_modules/react-native-reanimated/android/src/reactNativeVersionPatch/BorderRadiiDrawableUtils/latest/com/swmansion/reanimated/BorderRadiiDrawableUtils.java:19: error: incompatible types: CornerRadii cannot be converted to float
          computedBorderRadius.getTopLeft(),
                                         ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
3 errors
3 warnings