Jest: with the hook API, raw touch events fired under a `GestureDetector` don't reach ancestors under Testing Library 14

未关闭
#4,656 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

@aliassar 已经在做这个了。

开始于 2026年11月2日。

评估

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

调研方向

Start with the Jest mock at src/mocks/hostDetector.tsx and the hook detector at src/v3/detectors/NativeDetector.tsx, then reproduce the failing case from the linked repository with npx jest. Check how the mock and detector interact with Testing Library 14's touch event handling. Done when the parent receives touchStart in Jest under a hook gesture and the builder API case still passes.

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

描述

Platform: Android Platform: iOS
Description

This happens only in Jest: it's an interaction between Gesture Handler 3's test stand-in and React Native Testing Library, not a bug in the runtime.

Under Jest, jestSetup.js replaces HostGestureDetector with a plain View (src/mocks/hostDetector.tsx at v3.3.0). For a hook gesture, NativeDetector passes that View its onStartShouldSetResponder (NativeDetector.tsx#L76 at v3.3.0), which declines the responder.

Testing Library 14 treats a declining responder as blocking every event for the ancestors above it, raw touch events included. So fireEvent(child, 'touchStart') under a hook gesture's GestureDetector no longer reaches a parent's onTouchStart. On a device it does. (Once a gesture activates on a device, the ancestors get touchCancel, as expected: that isn't what this is about. In Jest, touchStart and the moves before activation never reach them.) With a builder-API gesture, no such View is rendered and the touch gets through, so tests that passed on 2.x start failing after moving to the hooks.

The root cause appears to be on Testing Library's side: on a device, raw touch events reach every ancestor whatever the responder does, and Testing Library applies the responder rule to them anyway. I've reported it there as callstack/react-native-testing-library#1957. I'm raising it here too because Gesture Handler 3 users will run into it when they move tests from the builder API to the hooks, so you may want to do something about it on your side, or at least be aware of it.

Steps to reproduce
  1. Clone https://github.com/jrmsayag/rntl-touch-bubbling-repro-demo and run npm ci.
  2. Run npx jest.
  3. "with a hook-API gesture (usePanGesture())" fails: the parent's onTouchStart gets 0 calls. "with a builder-API gesture (Gesture.Pan())" passes.
function Pannable() {
  const gesture = usePanGesture({});
  return (
    <GestureDetector gesture={gesture}>
      <View testID="child" />
    </GestureDetector>
  );
}

it('a raw touchStart under a hook gesture reaches the parent', async () => {
  const onTouchStart = jest.fn();
  await render(
    <GestureHandlerRootView>
      <View onTouchStart={onTouchStart}>
        <Pannable />
      </View>
    </GestureHandlerRootView>,
  );

  await fireEvent(screen.getByTestId('child'), 'touchStart');

  expect(onTouchStart).toHaveBeenCalledTimes(1); // fails: 0 calls
});
A link to a Gist, an Expo Snack or a link to a repository

https://github.com/jrmsayag/rntl-touch-bubbling-repro-demo

Gesture Handler version

3.3.0

React Native version

0.88.1

Platforms

Android, iOS (it happens in Jest, whatever the platform)

JavaScript runtime

Hermes (not relevant: Jest runs on Node)

Workflow

Using Expo Prebuild or an Expo development build (the repro itself is plain React Native with @react-native/jest-preset)

Build type

Debug mode

Device

Not applicable (Jest); pick any.

Device model

Not applicable (Jest, Node 22). @testing-library/react-native 14.1.0, Jest 30.5.2.

Acknowledgements

Yes

主要语言
TypeScript
星标
6.8k
派生
1.1k
平均合并
1 天 23 小时
30 天内合并 PR
46

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

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

software-mansion/react-native-gesture-handler 的其他 Issue

查看 software-mansion/react-native-gesture-handler 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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