Skip to content

Don't rely on inline requires to apply mocks in JS tests - #59307

Closed
robhogan wants to merge 1 commit into
mainfrom
robhogan/jest-mocks-without-inline-requires
Closed

robhogan wants to merge 1 commit into
mainfrom
robhogan/jest-mocks-without-inline-requires

Conversation

@robhogan

@robhogan robhogan commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary:

Test only change. RN's own Jest transform (jest/preprocessor.js, for this repo's own tests, not public) doesn't include babel-plugin-jest-hoist, so jest.mock calls run where they're written. Several tests only saw their mocks because inline requires deferred the require of 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:

  • Require modules under test after the jest.mock calls that affect them.
  • Use mock factories instead of spies or automocks where the code under test binds a function when it loads, or loads an ESM-only package (@octokit/rest).
  • Re-require modules after 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.

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]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 21, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 21, 2026
@javache

javache commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Will #59303 cause changes to our internal inline-requires transform?

@meta-codesync

meta-codesync Bot commented Oct 21, 2026

Copy link
Copy Markdown

@javache has imported this pull request. If you are a Meta employee, you can view this in D126757286.

@meta-codesync meta-codesync Bot closed this in a96b0e4 Oct 22, 2026
@react-native-bot

Copy link
Copy Markdown
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?

@react-native-bot react-native-bot added the Merged This PR has been merged. label Oct 22, 2026
@meta-codesync

meta-codesync Bot commented Oct 22, 2026

Copy link
Copy Markdown

@javache merged this pull request in a96b0e4.

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. Merged This PR has been merged. 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.

3 participants