upgrade-support icon indicating copy to clipboard operation
upgrade-support copied to clipboard

ReferenceError in jest unit tests after upgrading to RN 0.63.4

Open osartun opened this issue 4 years ago • 0 comments

Environment

System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
    Memory: 1.03 GB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.14.1 - /var/folders/hp/wl2yw33n69xf_1lkp8y6gry80000gn/T/yarn--1612349215809-0.819148110694037/node
    Yarn: 1.22.5 - /var/folders/hp/wl2yw33n69xf_1lkp8y6gry80000gn/T/yarn--1612349215809-0.819148110694037/yarn
    npm: 6.13.4 - /usr/local/Cellar/nvm/0.33.11/versions/node/v12.14.1/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.9.1 - /Users/oliver/.rbenv/shims/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 13.7, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK: Not Found
  IDEs:
    Android Studio: 3.5 AI-191.8026.42.35.5900203
    Xcode: 11.7/11E801a - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.7 - /usr/bin/javac
    Python: 3.6.4 - /Users/oliver/.pyenv/shims/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1
    react-native: 0.63.4 => 0.63.4
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Things I’ve done to figure out my issue

  • [x] I used upgrade-helper to do my upgrade.
  • [x] Found a way to fix the issue, but still have questions

Upgrading version

0.62.2 to 0.63.4

Description

After upgrading React Native our jest tests are failing with this error:

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

      at Object.get StatusBar [as StatusBar] (node_modules/react-native/index.js:118:12)
      at src/lib/NetworkActivityTracker.ts:35:27
      at reactionRunner (node_modules/mobx/lib/mobx.js:2112:9)
      at trackDerivedFunction (node_modules/mobx/lib/mobx.js:788:24)
      at Reaction.track (node_modules/mobx/lib/mobx.js:1813:22)

After some debugging it turns out that our tests seem to make actual API requests which resolve after the test has been torn down. The error message is related to these lines of code in our NetworkActivityTracker which calls setNetworkActivityIndicatorVisible on RN's StatusBar.

if (this.networkRequestCount === 0) {
  return StatusBar.setNetworkActivityIndicatorVisible(false);
}
return StatusBar.setNetworkActivityIndicatorVisible(true);

What I don't understand is how this relates to the React Native upgrade. Our tests shouldn't make API requests in the first place, sure, but why does this error only occur now?

Did anyone else have a similar issue?

Adding a mock for axios to prevent API calls from being made solves this issue, but still it must be related to a change within react-native which causes these errors to happen with this version and not with any other.

osartun avatar Feb 03 '21 11:02 osartun