Strange behavor when using `renderHook` with `wrapper` option
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- javascript, react
- 领域
- frontend, testing-qa
调研方向
从提供的 renderHook 复现开始,使用 wrapper 选项,并使用列出的 React、Jest 和 @testing-library/react 版本运行它。将 wrapper 和 hook 调用的结果与三个测试中的预期进行比较;当 wrapper 的行为符合这些断言时即表示完成。
由索引模型根据 Issue 内容生成。
描述
@testing-library/reactversion:14.3.1+15.0.7- Testing Framework and version:
{
"devDependencies": {
"@testing-library/react": "15.0.7"
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"react": "18.3.1"
}
}
Relevant code or config:
{
"engines": {
"node": "20",
"npm": "10",
"yarn": "PLEASE USE NPM"
}
}
What you did:
import { renderHook } from '@testing-library/react';
import React, { ReactElement } from 'react';
interface WrapperProps {
color?: string;
onInvoke?: jest.Mock;
children?: any;
}
let mockWrapperFn = jest.fn();
const Wrapper = (props?: WrapperProps): ReactElement => {
const bgColor = props?.color ?? 'black';
const onInvoke = props?.onInvoke ?? mockWrapperFn;
console.log(`GIVEN PROPS color: ${bgColor}`);
onInvoke(`INSIDE WRAPPER: ${bgColor}`);
return <div style={{ backgroundColor: bgColor }}>hello</div>;
};
const useHookTest = (onInvoke: jest.Mock) => {
onInvoke('INSIDE HOOK');
return 'hello-guy';
};
afterEach(() => jest.clearAllMocks());
test('not call hook with custom wrapper', () => {
const mockHookFn = jest.fn();
const { result } = renderHook(() => useHookTest(mockHookFn), {
wrapper: () => Wrapper({ color: 'red', onInvoke: mockWrapperFn }),
});
expect(mockWrapperFn).toHaveBeenCalledTimes(1);
expect(mockWrapperFn).toHaveBeenLastCalledWith('INSIDE WRAPPER: red');
expect(mockHookFn).toHaveBeenCalledTimes(1);
expect(mockHookFn).toHaveBeenLastCalledWith('INSIDE HOOK');
expect(result.current).toBe('hello-guy');
});
test('not call hook with default wrapper', () => {
const mockHookFn = jest.fn();
const { result } = renderHook(() => useHookTest(mockHookFn), { wrapper: Wrapper });
expect(mockWrapperFn).toHaveBeenCalledTimes(1);
expect(mockWrapperFn).toHaveBeenLastCalledWith('INSIDE WRAPPER: black');
expect(mockHookFn).toHaveBeenCalledTimes(1);
expect(mockHookFn).toHaveBeenLastCalledWith('INSIDE HOOK');
expect(result.current).toBe('hello-guy');
});
test('call hook without wrapper', () => {
const mockHookFn = jest.fn();
const { result } = renderHook(() => useHookTest(mockHookFn));
expect(mockWrapperFn).toHaveBeenCalledTimes(0);
expect(mockHookFn).toHaveBeenCalledTimes(1);
expect(mockHookFn).toHaveBeenLastCalledWith('INSIDE HOOK');
expect(result.current).toBe('hello-guy');
});
What happened:
renderHook does not render hook with wrapper option
- 主要语言
- JavaScript
- 星标
- 19.7k
- 派生
- 1.2k
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
testing-library/react-testing-library 的其他 Issue
-
fireEvent.select does not wrap its automatic native focus in act可能已有人在做 @sergioperezcheco 于 8 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
-
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict mode可能重新可做 @suhailopensource 于 79 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 35/100
testing-library/react-testing-library#1466 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 30/100
testing-library/react-testing-library#1459 · 2 条评论 ·
-
perf: optimize container lookup with early exit可能重新可做 @Ch-Abhinav-Chowdary 于 305 天前认领,目前没有进行中的 PR。 未关闭
难度 1/5 1 小时以内 新手友好度 35/100
testing-library/react-testing-library#1430 · 1 条评论 ·
-
`fireEvent.mouseEnter` does not forward `relatedTarget` (relatedTarget is the window instead)可能已有人在做 @swarnim02 于 322 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 55/100
查看 testing-library/react-testing-library 的全部 Issue
相似的 Issue
-
clawsweeper:fix-shape-clear clawsweeper:queueable-fix clawsweeper:source-repro impact:ux-friction issue-rating: 🦞 diamond lobster no-stale P2
难度 2/5 1-3 小时 新手友好度 65/100
openclaw/openclaw-enterprise#2571 · 1 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
fabriziosalmi/certmate#1247 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
bradymholt/cRonstrue#427 ·
维护者通常 1 天内回复
-
internal
难度 2/5 1-3 小时 新手友好度 70/100
frostney/GocciaScript#1715 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 2 天内回复