Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Support for React.cache function

Aperta
#1,407 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
javascript, node.js, react

Direzione di ricerca

Inizia eseguendo la riproduzione fornita di testCached.tsx, TestComponent.tsx e TestPage.tsx con React, Node 20 o 22, Vitest o Jest e jsdom. Controlla i limiti di supporto di package.json, quindi determina se React.cache può essere supportato in questo ambiente di test o se sono necessarie istruzioni documentate per il mocking; il lavoro è completato quando la riproduzione passa o la limitazione è chiaramente documentata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
JavaScript
Stelle
19.7k
Fork
1.2k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di testing-library/react-testing-library

Tutte le issue di testing-library/react-testing-library

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.