Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Support for React.cache function

Đang mở
#1,407 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Lĩnh vực
frontend, testing-qa

Hướng nghiên cứu

Bắt đầu bằng cách chạy bản tái hiện được cung cấp gồm testCached.tsx, TestComponent.tsx và TestPage.tsx với React, Node 20 hoặc 22, Vitest hoặc Jest và jsdom. Kiểm tra các giới hạn hỗ trợ trong package.json, sau đó xác định liệu React.cache có thể được hỗ trợ trong môi trường kiểm thử này hay cần tài liệu hóa hướng dẫn mocking; công việc được xem là hoàn tất khi bản tái hiện chạy thành công hoặc giới hạn được tài liệu hóa rõ ràng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
JavaScript
Star
19.7k
Fork
1.2k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của testing-library/react-testing-library

Tất cả issue của testing-library/react-testing-library

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.