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.

createAnimatedComponent is not a function when using mock

See original GitHub issue

I have created a component which I want to test, and the component uses react-native-reanimated to handle its animations. Per the documentation in mock.js, I have added the following line to __mocks__/react-native-reanimated.js:

jest.mock('react-native-reanimated', () => require('react-native-reanimated/mock'));

Expected Behavior

Tests will pass without error.

Actual Behavior

When I run my tests, they fail with the following error: TypeError: _reactNativeReanimated.default.createAnimatedComponent is not a function

Files

Component.js (imports and styles omitted for clarity):

const { Value, Clock, useCode, set, cond, eq, block, stopClock, multiply, interpolate } = Animated;
const Component = function(props: Props){
    const clock1 = new Clock(); // manages moving up
    const clock2 = new Clock(); // manages moving down
    const animState = new Animated.Value(0); // 0 = down, 1 = elevated
    const move = new Animated.Value(0);

    useCode(
        block([
            cond(eq(animState, 0), [stopClock(clock2), set(move, runTiming(clock1, move, {
                duration: 350,
                toValue: 0,
                easing: Easing.linear
            }))]),
            cond(eq(animState, 1), [stopClock(clock1), set(move, runTiming(clock2, move, {
                duration: 350,
                toValue: 1,
                easing: Easing.linear
            }))])
        ])
    )

    const translation = interpolate(move, {
        inputRange: [0, 1],
        outputRange: [0, -20]
    });

    return (
        <View style={props.style}>
            <TouchableWithoutFeedback onPressIn={() => animState.setValue(1)} onPressOut={() => animState.setValue(0)} onPress={props.onPress}>
                <Animated.View style={
                    [styles.card, 
                    {transform: [{translateX: translation}, {translateY: translation}]}
                ]}>
                    <ImageBackground source={props.image} style={styles.imageBackground} imageStyle={styles.cardImage}>
                            <Text style={styles.title}>Hi</Text>
                    </ImageBackground>
                </Animated.View>
            </TouchableWithoutFeedback>
        </View>
    )
}

export default Component;

Component.test.js:

import React from 'react';
import { fireEvent, render, wait } from '@testing-library/react-native';
import Component from '../../components/Component';

test('should call the onClick', () => {
    const clickFunction = jest.fn();
    const props = {
        onClick: clickFunction,
        image: require("/path/to/image.png")
    }
    const {getByText} = render(<Component {...props}/>)
    fireEvent.press(Component);
    expect(clickFunction.mock.calls.length).toBe(1);
})

Additionally, I have confirmed that jest is indeed using the provided mock, as I receive a different error when the mock is not used. Any thoughts? I’m guessing createAnimatedComponent needs to be added to the mock somehow.

Issue Analytics

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

github_iconTop GitHub Comments

11reactions
mwmcodecommented, Sep 20, 2019

👍

is it written in doc/readme that we can set up jset like this?

jest.mock('react-native-reanimated', () => require('react-native-reanimated/mock'));

Found about it by searching the issues, it’d be time-saving if it’s somewhere visible (can we update the PR to add it somewhere?)

8reactions
ggunticommented, Jun 19, 2020

In my __mocks__/react-native-reanimated.js file I have

jest.mock('react-native-reanimated', () => require('react-native-reanimated/mock'));

but when I execute npm run test, I get TypeError: _reactNativeReanimated.default.Value is not a constructor error.

"react": "16.11.0",
"react-native": "0.62.0",
"react-native-reanimated": "^1.8.0",
"jest": "^24.9.0",
Read more comments on GitHub >

github_iconTop Results From Across the Web

"TypeError" when trying to render react-native-reanimated ...
I just ran into this as well. I wound up needing to mock out the whole reanimated module with code like this:
Read more >
React Native: How to test Components implementing ...
Mocking Animated can work in some cases, but I've found implementing methods to time travel within a test to be faster and more...
Read more >
useAnimatedProps | React Native Reanimated
The animatedProps property is added when a native component is wrapped with Animated.createAnimatedComponent . If the animated props worklet uses any shared ...
Read more >
How to use Jest spyOn with React.js and Fetch - Meticulous
A spy may or may not mock the implementation or return value and just observe the method call and its parameters. On the...
Read more >
JavaScript react-native Animated.createAnimatedComponent ...
createAnimatedComponent extracted from open source projects. ... new TypeError("Super expression must either be null or a function, not "+typeof superClass) ...
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