6.1 更新jest.config.js
transformIgnorePatterns须包含@amazon-devices/react-native-kepler。程序包4.0.0会在its jest/setup.js文件中使用ES模块语法。如果不采用此模式,Jest文件会报错SyntaxError: Cannot use import statement outside a module。
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/StyleSheetreact-native/Libraries/StyleSheet/flattenStylereact-native/Libraries/EventEmitter/NativeEventEmitterreact-native/Libraries/Utilities/Platformreact-native/Libraries/Utilities/Dimensionsreact-native/Libraries/Utilities/PixelRatioreact-native/Libraries/Utilities/useWindowDimensionsreact-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 更新快照
修复所有测试问题后,重新生成快照。- 组件包装器结构更改
- 属性排序差异
- 文本节点渲染修改
- 异步边界更新
git diff审查快照更改内容。
