Fix @react-native/jest-preset failing under pnpm and Yarn pnpm-mode - #58489
Closed
amroaltah wants to merge 1 commit into
Closed
Fix @react-native/jest-preset failing under pnpm and Yarn pnpm-mode#58489amroaltah wants to merge 1 commit into
amroaltah wants to merge 1 commit into
Conversation
The preset failed in two ways under strict-isolation installs (pnpm and
Yarn pnpm-mode): `react-native` was not a declared dependency so
`jest-preset.js` could not resolve it, and the `transformIgnorePatterns`
rule only matched classic `node_modules` layouts so preset sources
shipped untransformed.
- Declare `react-native` as a peer dependency so the installer links it
into the preset's scope.
- Move `@babel/core` from dependencies to peerDependencies: `babel-jest`
peer-depends on it, so it must be provided, but as a direct dependency
a strict installer gives the preset its own copy and the consumer's
`babel.config.js` presets would then load under a different
`@babel/core` instance than the consumer's own. A peer keeps one copy,
matching how `react` and `react-native` are already declared.
`@babel/runtime` stays a direct dependency: the preset's sources are
compiled with `@babel/plugin-transform-runtime` helpers enabled, so the
transformed `jest/setup.js` requires `@babel/runtime/helpers/*` from
the preset's own scope at Jest runtime.
- Resolve the `babel-jest` transformer from the preset's own scope via
`require.resolve('babel-jest')` instead of the bare specifier.
- Match `react-native` packages at any nesting depth in
`transformIgnorePatterns` so pnpm (`.pnpm/...`) and Yarn pnpm-mode
(`.store/...`) layouts still transform preset and `react-native`
sources, while real `-suffix` packages stay ignored as before.
- Add two regression tests: an isolated-install test proving the preset
loads with only declared dependencies linked, and a transform test
proving the transformer resolves from the preset scope and the ignore
pattern covers pnpm/Yarn layouts without widening.
Known limitation: under Yarn pnpm-mode's `.store` layout, the
virtual-suffix allowance also matches packages like
`react-native-virtualized-view` whose names begin with `react-native-`,
so those get transformed too. The impact is performance-only (extra
transform work, no behavior change) and confined to Yarn pnpm-mode.
Fixes react#56641.
Changelog:
[General][Fixed] - Fix `@react-native/jest-preset` failing to resolve `react-native` and to transform preset sources under pnpm and Yarn pnpm-mode installs
Test Plan:
- Repo Jest suite over `packages/jest-preset`: 3 suites, 4 tests pass, including the two new regression tests.
- A real-pnpm strict-isolation repro (minimal monorepo installing the preset via `file:`, `hoist=false`): before the `transformIgnorePatterns` fix Jest fails with `Cannot use import statement outside a module`; with the fix the preset loads and Jest passes.
Author
|
Closing this PR — taking the change through internal review instead. Will link the internal diff here if it gets exported. |
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.
The preset fails in two ways under strict-isolation installs (pnpm and Yarn pnpm-mode):
react-nativeis not a declared dependency sojest-preset.jscannot resolve it, and thetransformIgnorePatternsrule only matches classicnode_moduleslayouts so preset sources ship untransformed.react-nativeas a peer dependency so the installer links it into the preset's scope.@babel/corefrom dependencies to peerDependencies:babel-jestpeer-depends on it, so it must be provided, but as a direct dependency a strict installer gives the preset its own copy and the consumer'sbabel.config.jspresets would then load under a different@babel/coreinstance than the consumer's own. A peer keeps one copy, matching howreactandreact-nativeare already declared.@babel/runtimestays a direct dependency: the preset's sources are compiled with@babel/plugin-transform-runtimehelpers enabled, so the transformedjest/setup.jsrequires@babel/runtime/helpers/*from the preset's own scope at Jest runtime.babel-jesttransformer from the preset's own scope viarequire.resolve('babel-jest')instead of the bare specifier.react-nativepackages at any nesting depth intransformIgnorePatternsso pnpm (.pnpm/...) and Yarn pnpm-mode (.store/...) layouts still transform preset andreact-nativesources, while real-suffixpackages stay ignored as before.Known limitation: under Yarn pnpm-mode's
.storelayout, the virtual-suffix allowance also matches packages likereact-native-virtualized-viewwhose names begin withreact-native-, so those get transformed too. The impact is performance-only (extra transform work, no behavior change) and confined to Yarn pnpm-mode.Fixes #56641.
Changelog:
[General][Fixed] - Fix
@react-native/jest-presetfailing to resolvereact-nativeand to transform preset sources under pnpm and Yarn pnpm-mode installsTest Plan:
packages/jest-preset: 3 suites, 4 tests pass, including the two new regression tests.file:,hoist=false): before thetransformIgnorePatternsfix Jest fails withCannot use import statement outside a module; with the fix the preset loads and Jest passes.