Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
arelstone opened this issue Feb 24, 2021 · 1 comment

Comments

@arelstone
Copy link

arelstone commented Feb 24, 2021

After implementing branch.io in my project I've installed this package input my project, to be able to gracefully handle deconstruction of the link. This works like a charm. Thanks

However when I run my tests i get a the following error

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

      at node_modules/react-native-url-polyfill/index.js:13:69
      at Object.getValue [as URL] (node_modules/react-native/Libraries/Utilities/defineLazyObjectProperty.js:43:16)
          at Array.forEach (<anonymous>)

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

      at node_modules/react-native-url-polyfill/index.js:16:23
      at Object.getValue [as URLSearchParams] (node_modules/react-native/Libraries/Utilities/defineLazyObjectProperty.js:43:16)
          at Array.forEach (<anonymous>)

In my code i import URL from this lib like this import { URL } from 'react-native-url-polyfill'

Should I write a mock for this? Does such a mock already exist?

A bit info about my system

npx envinfo --preset jest

  System:
    OS: macOS 11.2
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
  Binaries:
    Node: 12.13.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.12.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^26.6.3 => 26.6.3 

npx react-native info

info Fetching system and libraries information...
System:
    OS: macOS 11.2
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
    Memory: 118.13 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.13.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.12.0 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.10.0 - /Users/______/.rvm/gems/ruby-2.7.2/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.4, DriverKit 20.2, macOS 11.1, tvOS 14.3, watchOS 7.2
    Android SDK:
      API Levels: 30
      Build Tools: 29.0.2, 30.0.3
      System Images: android-30 | Google APIs Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 4.1 AI-201.8743.12.41.7042882
    Xcode: 12.4/12D4e - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_282 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/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
@arelstone
Copy link
Author

Sometimes you think you've tried everything and seems like creating an issue is the last result.

Gave this ago more.. 10 minutes after creating this i found a solution 🤦

I solved this by adding URL and URLSearchParams to globalThis in my jest setup

import { URL, URLSearchParams } from 'react-native-url-polyfill'

globalThis.URL = URL
globalThis.URLSearchParams = URLSearchParams

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant