react / react/react-native

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

未关闭
#56,641 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Needs: Attention Needs: Repro Needs: Version Info p: Microsoft Partner
主要语言
C++
星标
127k
派生
25.3k
平均合并
1 天 23 小时
30 天内合并 PR
4

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从必需的 rnx-kit reproducer 中的 @react-native/jest 依赖元数据和 .yarnrc.yml workaround 开始。移除 workaround,运行 yarn,运行 build-scope @rnx-kit/test-app,然后从 packages/test-app 运行测试,以复现 resolver 失败。完成的标准是测试无需 workaround 即可通过,并且依赖关系可在 pnpm 或 pnpm 模式的 Yarn 中正常工作。

由索引模型根据 Issue 内容生成。

评估

技术栈
react-native
领域
mobile, testing
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
52/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。