question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Out of memory error using debug() in an Animated.FlatList with a ref

See original GitHub issue

Describe the bug

I’m implementing a test in a component that renders an Animated.FlatList, when I use debug the test hangs and eventually throws a JavaScript heap out of memory error.

I found out that If I remove the ref I’m passing to the Animated.FlatList everything works fine. It also works with a regular FlatList.

Expected behavior

Test shouldn’t hang and debug() output should be visible in the console.

Steps to Reproduce

Run this test:

function MyFlatList() {
  const ref = useRef();
  return (
    <Animated.FlatList
      ref={ref}
      data={[1, 2, 3]}
      renderItem={({ item }) => <Text>{item}</Text>}
    />
  );
}

test('FlatList', () => {
  const { debug } = render(<MyFlatList />);
  debug();
  expect(true).toBe(true);
});

The test will hang and throw this error: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Versions

@testing-library/react-native: 7.0.2 => 7.0.2 
react: 16.13.1 => 16.13.1 
react-native: 0.63.2 => 0.63.2 
react-test-renderer: ^16.13.1 => 16.13.1 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
louiszawadzkicommented, Oct 1, 2020

Hi, we’ve faced a similar issue. It looks as if when using a ref on an Animated component, the snapshot becomes huge.

As an example, you can run:

import React, { useRef } from 'react';
import { Animated } from 'react-native';
import { render } from 'react-native-testing-library';

const Component = () => {
  const ref = useRef(null);
  return <Animated.View ref={ref} />;
};

describe('component', () => {
  it('renders', () => {
    const page = render(<Component />);
    expect(page).toMatchSnapshot();
  });
});

And see that the snaphsot is nearly 3,000 lines long. If you use an Animated.ScrollView instead of Animated.View, the snapshot is more than 30,000 lines long!

Also this might be linked to https://github.com/facebook/jest/issues/8109

0reactions
mdjastrzebskicommented, Aug 8, 2022

Seems like the root issue is related to the actual size of component tree generated by using Animated wrappers. If so then the you can work around the issue by:

  1. including memory allocated to jest instance using approach described by @raimonkh
  2. mocking Animated implementation as described by @scrungrth, preferably using jest features instead of Object.defineProperty
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I test to animated component in react-native with jest?
FlatList >. ( just ) So, This test code has success ! It shows me the result of debug(). How can i test...
Read more >
FlatList - React Native
A performant interface for rendering basic, flat lists, supporting the most handy features:
Read more >
Solving Memory Leaks with React Native - Enquero
An app provided to help reproduce the issue turned out to be leaking components via Animated loop that has been started in componentDidMount...
Read more >
TS animated Flatlist type error : r/reactnative - Reddit
createAnimatedComponent (FlatList); interface IArr { id: string; ... rNav]} /> </View> ) }; export default function App() { const translateX ...
Read more >
File System API — Emscripten 3.1.26-git (dev) documentation
All files exist strictly in-memory, and any data written to them is lost when the ... registerDevice() , a device node can be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found