react / react/react-native

`@react-native/jest` fails to resolve `react-native` in monorepos

Open
#56,641 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs: Attention Needs: Repro Needs: Version Info p: Microsoft Partner
Dominant language
C++
Stars
127k
Forks
25.3k
Avg merge
1d 23h
Merged PRs (30d)
4

Description

Description

@react-native/jest fails to resolve react-native in monorepos that use pnpm or Yarn in pnpm mode. @react-native/jest makes direct references to react-native but never declares it as a dependency, hence it cannot find it because the package manager will never install it.

I was going to add react-native as a peer dependency but it looks like this will create a cycle since react-native has a peer dependency on @react-native/jest. Not sure what approach you would prefer here.

This is the workaround we're currently using: https://github.com/microsoft/rnx-kit/blob/18bf2c4c307826d8d075351d1b8aa93387566177/.yarnrc.yml#L37-L40

Steps to reproduce
  1. Clone https://github.com/microsoft/rnx-kit
  2. Remove the workaround mentioned above
  3. Run yarn
  4. Build dependencies leading up to the test app: yarn build-scope @rnx-kit/test-app
  5. Run test: cd packages/test-app; yarn test
React Native Version

0.85.2

Affected Platforms

Other (please specify)

Output of npx @react-native-community/cli info
n/a
Stacktrace or Logs
% yarn test
 FAIL  test/App.test.tsx
  ● Test suite failed to run

    Cannot find module 'react-native/Libraries/AppState/AppState' from '../../node_modules/.store/@react-native-jest-preset-virtual-2eb6229c53/package/jest/mock.js'

      at Resolver._throwModNotFoundError (../../node_modules/.store/jest-resolve-npm-29.7.0-5c36f0eefb/package/build/resolver.js:427:11)
      at mock (../../node_modules/.store/@react-native-jest-preset-virtual-2eb6229c53/package/jest/mock.js:15:19)
      at Object.<anonymous> (../../node_modules/.store/@react-native-jest-preset-virtual-2eb6229c53/package/jest/setup.js:68:19)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.297 s
Ran all test suites.
MANDATORY Reproducer

https://github.com/microsoft/rnx-kit/tree/tido/bump-react-native

Screenshots and Videos

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the @react-native/jest dependency metadata and the .yarnrc.yml workaround in the mandatory rnx-kit reproducer. Remove the workaround, run yarn, build-scope @rnx-kit/test-app, and run the test from packages/test-app to reproduce the resolver failure. Done means the test passes without the workaround and the dependency relationship works with pnpm or Yarn in pnpm mode.

Written by the indexing model from the issue text.

Assessment

Tech stack
react-native
Domain
mobile, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.