as

Settings
Sign out
Notifications
Alexa
亚马逊应用商店
Ring
AWS
文档
Support
Contact Us
My Cases
新手入门
设计和开发
应用发布
参考
支持
原生
Skip to main content

6.1 更新jest.config.js

transformIgnorePatterns必须包含@amazon-devices/react-native-kepler。该程序包的某些4.0.x版本(例如4.0.1)在其jest/setup.js文件中使用ES模块语法。如果不采用此模式,Jest文件会失败并报错:SyntaxError: Cannot use import statement outside a module。 使用与Vega应用模板相同的Jest配置。该模板使用@amazon-devices/react-native-kepler预设并将react-native导入映射到@amazon-devices/react-native-kepler,如以下示例所示。
jest.setup.js中的模拟使用react-native/Libraries/...路径,例如:react-native/Libraries/Utilities/PixelRatio。moduleNameMapper会将这些路径解析为@amazon-devices/react-native-kepler中的匹配模块,因此您无需更改它们。如果您的项目已经从模板中拥有了jest.config.json文件,请将setupFilesAfterEnv添加到该文件中,而不是创建jest.config.js文件。

6.2 为电视模拟创建jest.setup.js

RN 0.83更改了模拟在测试环境中的工作方式。创建一个包含TV适用模拟的设置文件,如下例所示。

6.3 为.default导出修复Jest模拟

RN 0.83更改了许多内部模块以使用export default。现有的Jest模拟会返回不含.default属性的普通对象,该模拟会在静默状态下失败,导致组件在运行时收到undefined。 症状 在多个测试套件中Cannot read properties of undefined (reading 'create')。 受影响模块
  • react-native/Libraries/StyleSheet/StyleSheet
  • react-native/Libraries/StyleSheet/flattenStyle
  • react-native/Libraries/EventEmitter/NativeEventEmitter
  • react-native/Libraries/Utilities/Platform
  • react-native/Libraries/Utilities/Dimensions
  • react-native/Libraries/Utilities/PixelRatio
  • react-native/Libraries/Utilities/useWindowDimensions
  • react-native/Libraries/BatchedBridge/NativeModules
修复对象类型导出模式
修复函数类型导出模式:

6.4 使用act()包装更新测试文件

React 19要求测试中的状态更新必须包装在act()中,如下例所示。
@testing-library/react-native 如果您使用@testing-library/react-native,则测试库会在内部处理act()。请勿在ReactTestRenderer.act()中包装render(),因为这会导致“无法未挂载的测试渲染器上访问.root”错误。

6.5 修复导航栈计时器泄漏问题

如果使用react-navigation__stack,其Card.tsx文件中的动画setTimeout回调会在Jest拆除测试环境后触发。 症状: You are trying to import a file after the Jest environment has been torn down 修复: 添加到受影响的测试文件中

6.6 修复isolatedModules类型导出错误

如果TypeScript报告TS1205: Re-exporting a type when 'isolatedModules' is enabled requires using 'export type',请更新您的导出类型,如下例所示。

6.7 更新快照

修复所有测试问题后,重新生成快照。
审查更改。预期的React 19快照差异包括:
  • 组件包装器结构更改
  • 属性排序差异
  • 文本节点渲染修改
  • 异步边界更新
提交前使用git diff审查快照更改。
最后修改于 2026年10月28日