React-native-reanimated: jest testing - '_reactNativeReanimated.default.Value is not a constructor'

Created on 19 Jun 2020  路  10Comments  路  Source: software-mansion/react-native-reanimated

Description

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",

Screenshots

Captura虇 de ecran din 2020-06-19 la 21 18 00

Steps To Reproduce

  1. See Description above

Expected behavior

Run test without errors.

Actual behavior

Test fails with TypeError: _reactNativeReanimated.default.Value is not a constructor error.

Snack or minimal code example

My App-test.js file:

import 'react-native';
import React from 'react';
import App from '../src/App';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  renderer.create(<App />);
});

Package versions

  • React: 16.11.0
  • React Native: 0.62.0
  • React Native Reanimated: ^1.8.0

Most helpful comment

@jakub-gonet
Thanks! It works.
As you suggested, in my __mocks__/react-native-reanimated.js file I just replaced

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

with

module.exports = require('react-native-reanimated/mock');

All 10 comments

Update jest transformIgnorePatterns config like reference

Same error:
Captura虇 de ecran din 2020-06-22 la 15 26 25

Tried in 1.9.0 but error in still there.

Same issue here

What happens if you change Value to Value: AnimatedValue in node_modules/react-native-reanimated/mock.js?

@jakub-gonet
Same error.
Captura虇 de ecran din 2020-06-23 la 17 44 34

Oh, I didn't see you used it in the node_modules mock and not in the test.

You should use:

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

in the tests testing components that use functions from Reanimated.

Global mock should be used by adding to react-native-reanimated.js one line:

module.exports = require('react-native-reanimated/mock');

If this doesn't resolve your problem please respond.

@jakub-gonet
Thanks! It works.
As you suggested, in my __mocks__/react-native-reanimated.js file I just replaced

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

with

module.exports = require('react-native-reanimated/mock');

Hey guys!
Same issue here.
I have this line in my root folder __mocks__/react-native-reanimated.js
module.exports = require('react-native-reanimated/mock');

I added react-native-reanimated in the jest config as you can see here
Schermata 2020-07-07 alle 20 03 12

but I face always

TypeError: _reactNativeReanimated.Value is not a constructor
Schermata 2020-07-07 alle 20 04 04

I'using

"jest": "^24.9.0",
"jest-extended": "^0.11.2",
"jest-junit": "^10.0.0",
"react-native": "0.61.4",
"react": "16.9.0",

Any suggetion?
Thank you!

Figure out
that if I use in my code

static syntax like these example tests passed:
new Animated.Value(State.UNDETERMINED);
Animated.useCode(...
Animated.block([...
const zIndex = Animated.cond(Animated.eq(state, State.ACTIVE), 3, 0);

Maybe this can helps...

Was this page helpful?
0 / 5 - 0 ratings