react-native icon indicating copy to clipboard operation
react-native copied to clipboard

ExceptionsManager.handleException is not a function

Open eliw00d opened this issue 3 years ago • 14 comments

We recently upgraded to React Native 0.63, Jest 26, and React Native Testing Library 7. When running jest with --runInBand we have noticed the following error randomly in our CI:

/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:104
  ExceptionsManager.handleException(error, true);
                    ^
TypeError: ExceptionsManager.handleException is not a function
    at reportLogBoxError (/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:108:21)
    at Immediate.<anonymous> (/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:217:7)
    at processImmediate (internal/timers.js:456:21)

It appears that NativeExceptionsManager is mocked but handleException is not: https://github.com/facebook/react-native/blob/master/jest/setup.js#L42-L47

❯ npx envinfo --npmPackages react,react-native,react-test-renderer,@testing-library/react-native

  npmPackages:
    @testing-library/react-native: 7.0.2 => 7.0.2 
    react: 16.13.1 => 16.13.1 
    react-native: 0.63.2 => 0.63.2 
    react-test-renderer: 16.13.1 => 16.13.1 
❯ react-native info
info Fetching system and libraries information...
System:
    OS: macOS 10.15.6
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 746.34 MB / 32.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.18.3 - ~/.nvm/versions/node/v12.18.3/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.6 - ~/.nvm/versions/node/v12.18.3/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.9.3 - /Users/eliw00d/.rvm/gems/ruby-2.4.5/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 13.6, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK: Not Found
  IDEs:
    Android Studio: 3.6 AI-192.7142.36.36.6392135
    Xcode: 11.6/11E708 - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.4 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: Not Found
    react-native: 0.63.2 => 0.63.2 
  npmGlobalPackages:
    *react-native*: Not Found

eliw00d avatar Sep 03 '20 13:09 eliw00d

:warning: Missing Environment Information
:information_source: Your issue may be missing information about your development environment. You can obtain the missing information by running react-native info in a console.

github-actions[bot] avatar Sep 03 '20 14:09 github-actions[bot]

We're seeing the same issue repeatedly, trying to test Apollo mutations with react-native-testing-library.

(This issue seems to totally swallow exception messages, so it is very painful when trying to debug unit tests!)

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.

      at parseErrorStack (node_modules/react-native/Libraries/Core/Devtools/parseErrorStack.js:53:26)
      at Immediate._onImmediate (node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:192:48)

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.

      at reportLogBoxError (node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:85:27)
      at Immediate._onImmediate (node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:202:7)
mobile2mr/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:94

  ExceptionsManager.handleException(error, true);
                    ^

TypeError: ExceptionsManager.handleException is not a function
    at handleException (mobile2mr/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:108:21)
    at Immediate.reportLogBoxError (mobile2mr/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:217:7)
    at processImmediate (internal/timers.js:456:21)

Environment info:

System:
    OS: macOS 10.15.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
    Memory: 49.95 MB / 16.00 GB
    Shell: 5.5.1 - /usr/local/bin/zsh
  Binaries:
    Node: 12.18.3 - /usr/local/bin/node
    Yarn: 1.22.4 - ~/.yarn/bin/yarn
    npm: 6.14.6 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.9.3 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 13.7, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK:
      API Levels: 23, 26, 27, 28, 29
      Build Tools: 23.0.1, 26.0.1, 27.0.3, 28.0.3, 29.0.2
      System Images: android-23 | Intel x86 Atom_64, android-23 | Google APIs Intel x86 Atom, android-23 | Google APIs Intel x86 Atom_64, android-28 | Intel x86 Atom_64, android-30 | Google APIs Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 3.1 AI-173.4720617
    Xcode: 11.7/11E801a - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_171 - /usr/bin/javac
    Python: 2.7.15 - /usr/local/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1
    react-native: ^0.63.2 => 0.63.2
  npmGlobalPackages:
    *react-native*: Not Found

For now we can work around it just by moving this line to the top of LogBoxData.js. Jest seems to be erroring out when it hits the inline require:

const ExceptionsManager = require('../../Core/ExceptionsManager');

...And adding this mock to jest/setup.js in RN:

  .mock("../Libraries/Core/ExceptionsManager", () => {
    return {
      handleException: (...args) => {
        return;
      },
    };
  })

lukewlms avatar Sep 10 '20 20:09 lukewlms

Thanks for the issue @eliw00d!

It looks like @lukewlms has a solution. Do you mind submitting a PR to fix the issue in core?

safaiyeh avatar Sep 11 '20 16:09 safaiyeh

Sure, I'll submit what I have.

lukewlms avatar Sep 11 '20 18:09 lukewlms

Is there an update on this issue? This is blocking my upgrade. It occurs when we are running our test suite.

owencraston avatar Sep 21 '20 22:09 owencraston

Submitted PR: https://github.com/facebook/react-native/pull/30027

lukewlms avatar Sep 24 '20 22:09 lukewlms

what worked for me (@lukewlms's solution works too but requires forking rn) was using jest.useFakeTimers(); for each failing test (I only had 2)

KrumTy avatar Oct 23 '20 08:10 KrumTy

Appreciate submitting the PR!

safaiyeh avatar Oct 23 '20 23:10 safaiyeh

Commented on the PR - I'd like to take a look at this, could someone set up a minimal reproduction for me?

rickhanlonii avatar Oct 26 '20 16:10 rickhanlonii

For me, adding jest.useFakeTimers() fix the problem

  beforeEach(() => {
    jest.resetModules();
    jest.resetAllMocks();
    jest.useFakeTimers();  // <-- Add this
  });

rnike avatar Nov 27 '20 02:11 rnike

Dropping a note here on what worked for us when we encountered this issue. We did a mock of LogBox in our jest setup file like that:

jest.mock('react-native/Libraries/LogBox/LogBox')

lingumi-remy avatar Oct 07 '21 03:10 lingumi-remy

great @lingumi-remy it works!

GoMino avatar Oct 21 '21 13:10 GoMino

i have sama the problem `/Users//<ProjectName>/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:108 ExceptionsManager.handleException(error, true); ^

TypeError: ExceptionsManager.handleException is not a function at reportLogBoxError (/Users//<ProjectName>/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:108:21) at Immediate. (/Users//<ProjectName>/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:205:7)`

maybe you forget add sync on test, i am bad for test

example wrong test `describe('International Order snapshot test', () => { it('should render correctly', () => { const store = createStore(storeModel);

  const tree = render(
    <StoreProvider store={store}>
      <InternationalOrder route={mockRoute as never} navigation={mockNavigation as never} />
    </StoreProvider>,
    options,
  ).toJSON();
  expect(tree).toMatchSnapshot();
});

});`

and example true, like this : `describe('International Order snapshot test', () => { it('should render correctly', async () => { --> async const store = createStore(storeModel);

  const tree = render(
    <StoreProvider store={store}>
      <InternationalOrder route={mockRoute as never} navigation={mockNavigation as never} />
    </StoreProvider>,
    options,
  ).toJSON();
  await waitForRender(200); --> waiting render 200 ms
  expect(tree).toMatchSnapshot();
});

});`

this fix my issues

fandu139 avatar Apr 06 '22 05:04 fandu139

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Jul 09 '23 05:07 github-actions[bot]

This issue was closed because it has been stalled for 7 days with no activity.

github-actions[bot] avatar Jul 16 '23 05:07 github-actions[bot]