Skip to content

fix(jest): avoid loading native runtime in mock - #760

Open
huytdps13400 wants to merge 1 commit into
appandflow:mainfrom
huytdps13400:fix/550-jest-mock-native-module
Open

fix(jest): avoid loading native runtime in mock#760
huytdps13400 wants to merge 1 commit into
appandflow:mainfrom
huytdps13400:fix/550-jest-mock-native-module

Conversation

@huytdps13400

Copy link
Copy Markdown

Summary

The built-in Jest mock eagerly called jest.requireActual("react-native-safe-area-context"). Importing the mock therefore evaluated the package runtime and its StyleSheet/Dimensions dependencies, which reaches NativeDeviceInfo.getConstants() and fails in affected React Native 0.76 Jest environments.

This replaces the eager actual-package dependency with React-only mock contexts, hooks, provider, view, listener, and HOC implementations. The mock keeps the package's 13 runtime exports, preserves custom initialMetrics, forwards SafeAreaView props/ref to a test host component, and maps listener inset events without loading native code.

Fixes #550

Test Plan

  • RED regression: importing src/jest/mock while the react-native runtime throws failed at jest.requireActual -> SafeAreaContext -> react-native before the fix
  • yarn validate:jest --runInBand — 6 suites, 26 tests, 11 snapshots passed
  • yarn validate:typescript
  • yarn validate:eslint — no errors; 3 unchanged deep-import warnings outside this diff
  • yarn format:prettier:check
  • yarn prepare — CommonJS, ESM, and TypeScript declaration builds passed

@huytdps13400

Copy link
Copy Markdown
Author

Added an exact-version consumer verification beyond the repository suite:

  1. Packed this branch as the published package shape (jest/mock.js -> built CommonJS mock).
  2. Installed it in a clean consumer with react-native@0.76.1, react@18.3.1, the RN Jest preset, and the documented jest.mock("react-native-safe-area-context", () => mockSafeAreaContext) setup.
  3. Rendered SafeAreaProvider with custom metrics and read them through both hooks.

Result: Jest passed (1/1) with no NativeDeviceInfo.getConstants() error. This exercises the package entrypoint and the RN version from #550, not only the source-level harness.

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

Successfully merging this pull request may close these issues.

jest mock and RN 0.76.1: TypeError: NativeModule.getConstants is not a function

1 participant