Support for React.cache function
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 45/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- javascript, node.js, react
- 领域
- frontend, testing-qa
调研方向
首先,使用 React、Node 20 或 22、Vitest 或 Jest 以及 jsdom 运行提供的 testCached.tsx、TestComponent.tsx 和 TestPage.tsx 复现。检查 package.json 的支持范围,然后确定 React.cache 是否可以在此测试环境中得到支持,或者是否需要记录关于 mocking 的指导;当复现通过,或限制已得到明确记录时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
@testing-library/reactversion: 16.3.0- Testing Framework and version: Tested in
[email protected]and[email protected] - DOM Environment:
[email protected] - Node 20 or 22
Relevant code or config:
// testCached.tsx
import { cache } from 'react';
export default cache((data: number) => {
return Math.random() * data;
});
----------------------------------------------------------------------
// TestComponent.tsx
import myCachedFunction from "./testCached";
import React from "react";
export const TestComponent = () => {
return (
<div>
<p data-testid="cached-value-component">{myCachedFunction(200)}</p>
</div>
);
};
----------------------------------------------------------------------
// TestPage.tsx
import { TestComponent } from "./TestComponent";
import myCachedFunction from "./testCached";
import React from "react";
export function TestPage() {
return (
<div>
<p data-testid="cached-value-page">{myCachedFunction(200)}</p>
<TestComponent />
</div>
);
}
----------------------------------------------------------------------
// Testfile
it("Should use the cached value", () => {
render(<TestPage />);
expect(screen.getByTestId("cached-value-page").textContent).toEqual(
screen.getByTestId("cached-value-component").textContent
);
});
What you did:
We are using the React.cache function but cannot get it to work in tests.
What happened:
In tests, it does not use the cache but simply returns a new value for each call. When running the above code in nextjs 14 it works as expected and returns the cached value instead of a new value displaying the same value in TestComponent and TestPage.
For example the tests return:
Expected: "158.69893572775896"
Received: "45.853220887195164"
Also worth mentioning i experimented with different solutions, for example some in this issue, but ended up having it mocked.
Reproduction:
Problem description:
Suggested solution:
It should work as expected, or at least mention in docs to mock it
- 主要语言
- 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 于 3 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
-
bug: calling configure() without reactStrictMode resets it to undefined, silently disabling strict mode可能重新可做 @suhailopensource 于 73 天前认领,目前没有进行中的 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 于 299 天前认领,目前没有进行中的 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 于 316 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 55/100
查看 testing-library/react-testing-library 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 72/100
yjh051108/dsh-routing-suite#216 ·
-
bug user-priority/P2
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 4 天内回复
-
bug confirmed perf
难度 2/5 1-3 小时 新手友好度 72/100
videojs/video.js#9400 · 1 条评论 ·
维护者通常 1 天内回复
-
agent/scanner bug hive/hosted-available-lke648397-260827-5n31
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复