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

Fix lazy import error from jest and Appearance.js #47629

Closed

Conversation

Kudo
Copy link
Contributor

@Kudo Kudo commented Nov 15, 2024

Summary:

currently running jest test, it shows an error:

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From __tests__/App.test.tsx.

      at getState (node_modules/react-native/Libraries/Utilities/Appearance.js:18:26)
      at addChangeListener (node_modules/react-native/Libraries/Utilities/Appearance.js:71:19)
      at subscribe (node_modules/react-native/Libraries/Utilities/useColorScheme.js:10:66)
      at subscribeToStore (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:6232:10)
      at commitHookEffectListMount (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:13038:26)
      at commitPassiveMountOnFiber (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14461:11)
      at commitPassiveMountEffects_complete (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14421:9)
      at commitPassiveMountEffects_begin (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14408:7)
      at commitPassiveMountEffects (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14396:3)
      at flushPassiveEffectsImpl (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:16287:3)
      at flushPassiveEffects (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:16236:14)
      at node_modules/react-test-renderer/cjs/react-test-renderer.development.js:16051:9
      at workLoop (node_modules/scheduler/cjs/scheduler.development.js:266:34)
      at flushWork (node_modules/scheduler/cjs/scheduler.development.js:239:14)
      at Immediate.performWorkUntilDeadline [as _onImmediate] (node_modules/scheduler/cjs/scheduler.development.js:533:21)

it is a regression from #46123 that to have a lazy require.

this pr tries to mock useColorScheme to return light. i think we don't necessarily test the color scheme changes in jest runtime. originally useColorScheme also returns light because of this statement

Changelog:

[GENERAL] [FIXED] - Fixed jest error from Appearance.js

Test Plan:

$ npx @react-native-community/cli init RN0762 --pm bun --version 0.76.2
$ cd RN0762
$ bun test run

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Contributor A React Native contributor. p: Expo Partner: Expo Partner labels Nov 15, 2024
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 15, 2024
@Kudo
Copy link
Contributor Author

Kudo commented Nov 15, 2024

we can either mock https://github.com/facebook/react-native/blob/9a60038a40e16925ea1adeb3e3c937c22a615485/packages/react-native/Libraries/Utilities/Appearance.js. but that feels more complex to mock getColorScheme(), setColorScheme(), addChangeListener() to correct behavior.

Copy link
Member

@huntie huntie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks :)

@facebook-github-bot
Copy link
Contributor

@huntie has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@yungsters
Copy link
Contributor

Thanks for the fix. Can you also add a Jest unit test that would fail without this fix?

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Nov 21, 2024
@facebook-github-bot
Copy link
Contributor

@huntie merged this pull request in ce838a4.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @Kudo in ce838a4

When will my fix make it into a release? | How to file a pick request?

@Kudo Kudo deleted the @kudo/fix-jest-lazy-NativeAppearance branch November 21, 2024 16:23
@Kudo
Copy link
Contributor Author

Kudo commented Nov 21, 2024

Can you also add a Jest unit test that would fail without this fix?

@yungsters will try that in a separate pr. thanks!

facebook-github-bot pushed a commit that referenced this pull request Nov 28, 2024
Summary:
add a jest test to test when `useColorScheme` is not mocked. following up #47629 (comment)

## Changelog:

[GENERAL] [ADDED] - Add useColorScheme mock test

Pull Request resolved: #47988

Test Plan: ci passed

Reviewed By: javache

Differential Revision: D66573172

Pulled By: blakef

fbshipit-source-id: 820227f6fc4e18a968b3181fad8f534a716f1e9e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Contributor A React Native contributor. Merged This PR has been merged. p: Expo Partner: Expo Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants