How to fix “TypeError: global.__reanimatedWorkletInit is not a function”? Mainly this is happening when we write unit tests with jest-react native. So if you are using react-native-reanimated when you try to run your test suite you will get this error if you are using reanimated APIs. If you have already set up jest with react native then this is for you.
Steps to fix “TypeError: global.__reanimatedWorkletInit is not a function”
Once before all, you have to clear the cache and check whether the issue is still happening. you can run react-native start --reset-cache
. Then if the issue persists you can add the below code steps to your jest setup file.
Method 1
First, you try to add this below line to your jest setup file and rerun the tests.
global.__reanimatedWorkletInit = () => {};
After this, if you are getting more errors from reanimated module try the below method. In that we are going to mock mostly used react native reanimated APIs.
Method 2
To fix this issue we have to mock reanimated module in the jestSetup.js
file. If you are working with the reanimated package you will be running into these issues when you writing your tests. So add the given below code to your jestSetup.js
file so that issue will be fixed.
After you add these changes simply run your tests again and check.
Why do we mock external dependencies?
To simulate a module’s behaviour is to mock it. When the procedure you are testing has complicated external dependencies that would be challenging to include in a unit test, which is necessary. Through a mock function, jest handles mocking. The following code creates a mock function: jest.fn ()
. The implementation of this function, its return value, and assertions can then be specified.
If you have any questions or comments — feel free to comment here or reach out at [email protected]. Thank you.
Leave A Comment