Support for React.cache function
Opened this issue · 3 comments
@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
React.cache only works in the react-server environment which React Testing Library doesn't support yet. A version of RTL for React Server Components needs design first: #1209
I might have a solution for this particular problem - just mock React.cache.
This will not help with real Server Components, but can close this particular gap.
In very depths of React.cache there is a tight connecting between cache and the current request in flight. Without such request there is no cache (this line)
However it's more than possible to mock React.cache implementation and get desired behavior at least in testing environment, here is an example - https://codesandbox.io/p/devbox/silly-water-rcy35m?file=%2Fsrc%2F__tests__%2Ftest.js%3A13%2C1&workspaceId=ws_JL5wQXXoo2mn6XmDb9aeub
and a bit of documentation - https://github.com/theKashey/kashe?tab=readme-ov-file#reactcache-style-per-request-caching
You shouldn't mock React.cache. React Testing Library has no support for React Server Components at the moment so you shouldn't mock away RSC specific APIs. The test results you'd receive would not reflect the actual functionality of the tested Component.