jest/preprocessor.js transforms packages/metro-config with sourceType: 'script' and leaves its import statements, so no Jest test in the repository can require @react-native/metro-config

未關閉
#60,029 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 2 天內回覆

@lazerg 已經在處理了。

開始於 2026年11月16日。

  • #60035 來自 @lazerg —— 未關閉

評估

難度
3/5
預估耗時
1-2 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
javascript, react-native

研究方向

Read jest/preprocessor.js around the nodeFiles match and Babel options, then compare how those options transform packages/metro-config/src/index.flow.js. Run the reproducer linked in the issue to confirm the failing Jest require and check the output for each sourceType. Done means the test can require @react-native/metro-config while the matched files still receive the Node transform.

由索引模型根據 Issue 內容生成。

描述

API: Transforms Needs: Triage :mag:
Description

A Jest test in this repository that requires @react-native/metro-config fails with SyntaxError: Cannot use import statement outside a module in packages/metro-config/src/index.flow.js.

packages/metro-config/ matches nodeFiles in jest/preprocessor.js, whose files go through metro-babel-register's Babel options with sourceType: 'script'. Those options leave the import and export statements of src/index.flow.js, which src/index.js requires, in place. Under sourceType: 'module' or 'unambiguous' the same options turn every one of them into a require call (step 3 below).

Expected: the test passes, and the files nodeFiles matches keep the Node transform.

Steps to reproduce
  1. Clone react/react-native and run yarn install in it.
  2. Clone the reproducer and run its script on that checkout:
    git clone https://github.com/YevheniiKotyrlo/rn-jest-preprocessor-metro-config-repro
    ./rn-jest-preprocessor-metro-config-repro/jest-preprocessor/reproduce.sh react-native
    
    It copies this test to packages/react-native/src/private/__repro_metro_config__/__tests__/metro-config-from-source-test.js, runs yarn jest on it and removes it again:
    it('loads @react-native/metro-config from source', () => {
      const {getDefaultConfig} = require('@react-native/metro-config');
      expect(typeof getDefaultConfig).toBe('function');
    });
    
  3. The test fails with SyntaxError: Cannot use import statement outside a module at packages/metro-config/src/index.flow.js:13. The script then runs that file through the same Babel options under each sourceType:
    metro-babel-register 0.88.0
    sourceType: 'script'       import/export statements left: 5, require calls: 1
    sourceType: 'module'       import/export statements left: 0, require calls: 3
    sourceType: 'unambiguous'  import/export statements left: 0, require calls: 3
    

The script exits 1 while the SyntaxError shows, 0 once the test passes, and 2 when a step fails or the test fails another way. It ends:

SUMMARY: defect shows on react/react-native e37dbfd9561: a Jest test that requires @react-native/metro-config fails with "SyntaxError: Cannot use import statement outside a module" in packages/metro-config/src/index.flow.js, whose import statements jest/preprocessor.js left in place
React Native Version

main (nightly) at e37dbfd95615b83d525bfaa99ddd3915c1bb2820

Affected Platforms

Build - Linux, Build - Windows

Output of npx @react-native-community/cli info
react/react-native e37dbfd9561 (2026-11-16), Node v26.12.1, Yarn 1.22.22, Jest 29.7.0, Windows_NT 10.0.26300 x64
react/react-native e37dbfd9561 (2026-11-16), Node v24.22.2, Yarn 1.22.22, Jest 29.7.0, Linux 6.18.58.1-microsoft-standard-WSL2 x64
Stacktrace or Logs
<react-native>\packages\metro-config\src\index.flow.js:13
import INTERNAL_CALLSITES_PATTERNS from './internalCallsites.json';
^^^^^^

SyntaxError: Cannot use import statement outside a module

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
MANDATORY Reproducer

https://github.com/YevheniiKotyrlo/rn-jest-preprocessor-metro-config-repro

Screenshots and Videos

No response

主要語言
C++
星號
127k
分支
25.5k
平均合併
3 天 22 小時
30 天內合併 PR
235

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

react/react-native 的其他 Issue

查看 react/react-native 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。