Repository navigation
Conversation
React Native's Jest transform doesn't hoist `jest.mock` calls, and several tests only saw their mocks because inline requires deferred `require`s until first use. That holds for destructured requires only while the Babel preset lowers destructuring, which it does in this config as a side effect of lowering generators with regenerator. These tests now install mocks before the modules under test load, or load those modules after mocking, so they pass whether or not destructuring is lowered: - Require modules under test after the top-level `jest.mock` calls that affect them. - Use mock factories instead of spies or automocks where the code under test binds a function, or loads an ESM-only package, when it first loads. - Reload modules on each call or after `jest.resetModules()` where tests reset the registry. Changelog: [Internal]
javache
approved these changes
Oct 21, 2026
Contributor
|
Will #59303 cause changes to our internal inline-requires transform? |
|
@javache has imported this pull request. If you are a Meta employee, you can view this in D126757286. |
Collaborator
|
This pull request was successfully merged by @robhogan in a96b0e4 When will my fix make it into a release? | How to file a pick request? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Test only change. RN's own Jest transform (
jest/preprocessor.js, for this repo's own tests, not public) doesn't includebabel-plugin-jest-hoist, sojest.mockcalls run where they're written. Several tests only saw their mocks because inline requires deferred therequireof the module under test (or of its dependencies) until first use.That only works for destructured requires like
const {foo} = require('./foo')while the Babel preset lowers destructuring, which in this config it currently does only as a side effect of lowering generators with regenerator. #59303 removes that, and 114 tests across these 16 suites fail.This makes the tests install their mocks before the modules under test load, so they pass either way, without diverging Jest's transform from the app's:
jest.mockcalls that affect them.@octokit/rest).jest.resetModules()where tests reset the registry (publish-npm,Animated).The release script tests in particular are dangerous when their mocks silently stop applying - locally, they ran the real version bump against the checkout.
Changelog:
[INTERNAL] [CHANGED] - Don't rely on inline requires to apply mocks in JS tests
Test Plan:
The full Jest suite passes on this PR (6,284 tests), and with #59303 applied on top (6,285), both under Node 24 with
--no-cache. Without this PR, #59303 fails 114 tests in 16 suites, matching its CI.