Tests fail for missing Turbo Modules when using`requireActual` of `react-native`
Description
Problem
Upon mocking measureInWindow, I ran into an issue where my jest test did not pass unless I mocked the TurboModuleRegistry via jest.mock("react-native/Libraries/TurboModule/TurboModuleRegistry"); Adding the missing DevMenu module to React Native's jest setup for NativeModules allows the test to pass (more details below). I'm curious as to why, since the error is coming from the TurboModuleRegistry.
Reproducer
https://github.com/frankcalise/jest-mock-dev-menu
Setup
The mock looked like this
// Define the callback type for measureInWindow
type MeasureInWindowCallback = (x: number, y: number, width: number, height: number) => void;
// Create a mock for measureInWindow
const mockMeasureInWindow = jest.fn<void, [MeasureInWindowCallback]>((callback) => {
callback(10, 20, 100, 50); // Default mocked values
});
// Mock the View component to include measureInWindow
jest.mock("react-native", () => {
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const RN = jest.requireActual<typeof import("react-native")>("react-native");
return {
...RN,
Dimensions: {
get: jest.fn().mockReturnValue({ width: 750, height: 1334 }),
},
View: class extends RN.View {
measureInWindow = mockMeasureInWindow;
},
NativeEventEmitter: jest.fn().mockImplementation(() => ({
addListener: jest.fn(),
removeAllListeners: jest.fn(),
removeListener: jest.fn(),
removeSubscription: jest.fn(),
})),
};
});
Error
Test suite failed to run
Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'DevMenu' Could not be found
Workaround
By defining DevMenu inside of node_modules/react-native/jest/setup.js in the NativeModules mock, the test begins passing.
I'm happy to help fix this with the proper guidance on what a real solution looks like
Steps to reproduce
View minimal reproducer at https://github.com/frankcalise/jest-mock-dev-menu
React Native Version
0.77.2
Affected Platforms
Other (please specify)
Output of npx @react-native-community/cli info
System:
OS: macOS 15.4.1
CPU: (10) arm64 Apple M1 Pro
Memory: 126.09 MB / 32.00 GB
Shell:
version: "5.9"
path: /bin/zsh
Binaries:
Node:
version: 22.13.1
path: ~/.asdf/installs/nodejs/22.13.1/bin/node
Yarn:
version: 1.22.22
path: ~/.asdf/installs/nodejs/22.13.1/bin/yarn
npm:
version: 10.9.2
path: ~/.asdf/plugins/nodejs/shims/npm
Watchman: Not Found
Managers:
CocoaPods:
version: 1.15.2
path: /Users/fcalise/.asdf/shims/pod
SDKs:
iOS SDK:
Platforms:
- DriverKit 24.4
- iOS 18.4
- macOS 15.4
- tvOS 18.4
- visionOS 2.4
- watchOS 11.4
Android SDK: Not Found
IDEs:
Android Studio: 2024.2 AI-242.23339.11.2421.12700392
Xcode:
version: 16.3/16E140
path: /usr/bin/xcodebuild
Languages:
Java:
version: 17.0.9
path: /Users/fcalise/.asdf/shims/javac
Ruby:
version: 3.3.5
path: /Users/fcalise/.asdf/shims/ruby
npmPackages:
"@react-native-community/cli": Not Found
react:
installed: 18.3.1
wanted: 18.3.1
react-native:
installed: 0.77.2
wanted: 0.77.2
react-native-macos: Not Found
npmGlobalPackages:
"*react-native*": Not Found
Android:
hermesEnabled: Not found
newArchEnabled: Not found
iOS:
hermesEnabled: Not found
newArchEnabled: Not found
Stacktrace or Logs
Test suite failed to run
Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'DevMenu' Could not be found
MANDATORY Reproducer
https://github.com/frankcalise/jest-mock-dev-menu
Screenshots and Videos
No response
Thanks for resolution on this one, i was stuck with this one from yesterday. It is an actual bug and your solution from repo works! 🚀
Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'DevMenu' could not be found.
Now, even if you fix DevMenu, the same issue occurs with ToastAndroid for me. Can't hack around it the same way afaik.