Skip to content

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

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

Closed
IPWright83 opened this issue Jan 25, 2021 · 4 comments

Comments

@IPWright83
Copy link

IPWright83 commented Jan 25, 2021

Describe the bug

Getting lots of warnings when running tests with Jest:

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:95:27)
 at Immediate._onImmediate (node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:201:7)

/home/iw***/src/mobileApp/myuoy/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:104
ExceptionsManager.handleException(error, true);

I believe that this is likely this library due to testing-library/native-testing-library#132 but I may be wrong

Expected behavior

No warnings

Steps to Reproduce

I'm not exactly sure, as I don't know where this is being triggered from. The stack trace is originating within LogBoxData.

Versions

npmPackages:
    @testing-library/react-native: ^7.1.0 => 7.1.0 
    react: 16.13.1 => 16.13.1 
    react-native: 0.63.2 => 0.63.2 
    react-test-renderer: 16.13.1 => 16.13.1 
@thymikee
Copy link
Member

This is not related to this library. It's something that likely should be mocked in the official react-native jest preset. Feel free to propose such change. Meanwhile you should be good with e.g. this:

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

@IPWright83
Copy link
Author

Thanks @thymikee - especially for the suggestion rather than just a close.

@thymikee
Copy link
Member

Yea, it's a bummer it's not properly mocked in the source, but I believe it should be done there, instead of just patching it in this library. That's why I encourage folks to target their efforts to the right place.

@sabun123
Copy link

@thymikee, I just wanted to thank you for the mock you suggested. It solved my issue.

I was previously on React Native 0.63.3 with @testing-library/react-native version 7.1.0 and my jest unit tests worked fine. After upgrading to React Native 0.64 and React 17.0.1 I started experiencing Jest failing the entire suite with :

/Users/USER/Documents/code/project-name/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:104
  ExceptionsManager.handleException(error, true);
  
  TypeError: ExceptionsManager.handleException is not a function
    at reportLogBoxError (/Users/USER/Documents/code/project-name/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:108:21)
    at Immediate.<anonymous> (/Users/USER/Documents/code/project-name/node_modules/react-native/Libraries/LogBox/Data/LogBoxData.js:214:7)

After adding the following lines to my jest/setup.js file, everything works again!

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

It's unfortunate that an upgrade to React Native can still break unit tests at a fundamental level like this, but your answer is a lifesaver for those of us in production work.

anitsirk pushed a commit to MaharaProject/mahara-mobile-react-native that referenced this issue Nov 8, 2021
karlknowit added a commit to helsingborg-stad/app-mitt-helsingborg that referenced this issue Nov 24, 2021
anitsirk pushed a commit to MaharaProject/mahara-mobile-react-native that referenced this issue Mar 7, 2022
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

3 participants