testing-library / testing-library/react-testing-library

Support for React.cache function

Open
#1,407 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
19.7k
Forks
1.2k
Avg merge
3d 16h
Merged PRs (30d)
1

Description

  • @testing-library/react version: 16.3.0
  • Testing Framework and version: Tested in vitest@3.2.4 and jest@29.7.0
  • DOM Environment: jsdom@26.1.0
  • 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided testCached.tsx, TestComponent.tsx, and TestPage.tsx reproduction with React, Node 20 or 22, Vitest or Jest, and jsdom. Check package.json support boundaries, then determine whether React.cache can be supported in this test environment or needs documented mocking guidance; done means the reproduction passes or the limitation is clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, react
Domain
frontend, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.