Upgrading to RN 0.65.1 causes Jest to fail with TypeError: _reactNative.Keyboard.emit is not a function
See original GitHub issuePlease provide all the information requested. Issues that do not follow this format are likely to stall.
Description
After upgrading to React Native 0.65.1, Jest fails with tests that use certain React Native functionality. The errors it throws when failing:
TypeError: _reactNative.Keyboard.emit is not a function
TypeError: _reactNative.AppState.removeEventListener is not a function
Additionally, other tests are now suddenly failing with the following as well:
thrown: "Exceeded timeout of 5000 ms for a hook.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
React Native version:
0.65.1 (coming from 0.64.2)
Steps To Reproduce
Provide a detailed list of steps that reproduce the issue.
- Come from React Native 0.64.2 and upgrade to RN 0.65.1
- Run Jest and use functionality from the React Native package
- Watch Jest fail being unable to identify the React Native functionality
Expected Results
These Jest tests worked and all PASS in 0.64.2. The only change is upgrading to 0.65.1 to cause this to fail.
Unit test Jest code example
import { render } from '@testing-library/react-native';
import React from 'react';
import { Keyboard } from 'react-native';
import TestComponent from '../TestComponent';
describe('screens/testscreen/TestComponent', () => {
it('renders correctly', () => {
const setup = render(<TestComponent />);
expect(setup).toBeTruthy();
act(() => {
Keyboard.emit('keyboardDidShow', {});
Keyboard.emit('keyboardDidHide', {});
});
});
});
What can I try to get this working again, or is React Native 0.65.1 broken in this regard?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Mocking & monitoring Keyboard events with jest in react native
As of React Native 65, jest fails with: TypeError: _reactNative.Keyboard.emit is not a function. I have not yet found a solution for this....
Read more >Continuous integration for React applications using Jest and ...
I will show you how to configure continuous integration with CircleCI to automate the testing and ensure that any new code you add...
Read more >Mocking RN modules | React Made Native Easy
Most of the React Native specific utilities are automatically mocked for us since we are using react-native preset. If you install any other...
Read more >React Testing Library and the “not wrapped in act” Errors
Why do I get this error? In test, the code to render and update React components need to be included in React's call...
Read more >cannot read properties of undefined (reading 'html') at new ...
js and React. Trying to configure jest to run test suites the api Node works fine but I got the following error on...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Awesome, really appreciate the guidance @yungsters. That did the trick, I had no idea about
RCTDeviceEventEmitter
.For others who may read this. Before:
The replacement that works in RN 0.65.1 for us After:
For the AppState.removeEventListener, whilst it worked in code, Jest would not let it pass. In the end I replaced all of them with the .remove() function instead.
Before:
After:
Thanks again @yungsters! Closing this as solved
Thanks for the detailed summary, @sabun123!
I do wonder why
AppState.removeEventListener
isn’t working for you, though. Hmm…Nonetheless, best to migrate away from it since it is deprecated and will be removed in the future.