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.

Upgrading to RN 0.65.1 causes Jest to fail with TypeError: _reactNative.Keyboard.emit is not a function

See original GitHub issue

Please 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.

  1. Come from React Native 0.64.2 and upgrade to RN 0.65.1
  2. Run Jest and use functionality from the React Native package
  3. 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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
sabun123commented, Sep 29, 2021

Awesome, really appreciate the guidance @yungsters. That did the trick, I had no idea about RCTDeviceEventEmitter.

For others who may read this. Before:

import { Keyboard } from 'react-native';

// later in the actual test
act(() => {
      Keyboard.emit('keyboardDidShow', {});
      Keyboard.emit('keyboardDidHide', {});
    });

The replacement that works in RN 0.65.1 for us After:

import RCTDeviceEventEmitter from 'react-native/Libraries/EventEmitter/RCTDeviceEventEmitter';

// later in the actual test
act(() => {
      RCTDeviceEventEmitter.emit('keyboardDidShow', {});
      RCTDeviceEventEmitter.emit('keyboardDidHide', {});
    });

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:

  useEffect((): (() => {}) => {
    AppState.addEventListener('change', handleAppStateChange);

    return () => {
      AppState.removeEventListener('change', handleAppStateChange);
    };
  });

After:

  let appstatelistener;

  useEffect((): (() => {}) => {
    appstatelistener = AppState.addEventListener('change', handleAppStateChange);

    return () => {
      appstatelistener.remove();
    };
  });

Thanks again @yungsters! Closing this as solved

0reactions
yungsterscommented, Sep 29, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

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