testing-library / testing-library/react-testing-library
Support for React.cache function
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/reactversion: 16.3.0- Testing Framework and version: Tested in
vitest@3.2.4andjest@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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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