TypeError: (0 , _react.cache) is not a function #49304
Replies: 7 comments 11 replies
-
I figure out a way to solve this problem with const nextJest = require('next/jest');
// Providing the path to your Next.js app which will enable loading next.config.js and .env files
// @ts-ignore
const createJestConfig = nextJest?.default({ dir: './' }) || nextJest({ dir: './' });
// Any custom config you want to pass to Jest
const customJestConfig = {
// setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
// 🧑🏻🔧 FIX Problem here
// 🧑🏻🔧 resolve react module with the next.js inset one.
react: 'next/dist/compiled/react/cjs/react.development.js',
'^@/(.*)$': '<rootDir>/src/$1',
},
};
const configs = createJestConfig(customJestConfig);
module.exports = configs; |
Beta Was this translation helpful? Give feedback.
-
It was annoying for me as well. I did not used react canary version, just decided to bypass cache only for test and this is how i handled, in case somebody need it
const isNotTest = process.env.NODE_ENV !== "test";
function testCache<T extends Function>(func: T) {
return func;
}
const cachedFunc = isNotTest ? cache : testCache; and just use cachedFunc to which ever function you want to apply export const test= cachedFunc(() => {
...
}); |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
pls help me |
Beta Was this translation helpful? Give feedback.
-
I am experiencing this error even though I am not using tests with jest or anything like that. I am not in a testing context by the way as this issue describes but I am in a normal application context on a page that imports a function that uses the cache function from react |
Beta Was this translation helpful? Give feedback.
-
I decided not to mock but to use React Cache only in a Next.js environment by checking NEXT_RUNTIME:
|
Beta Was this translation helpful? Give feedback.
-
I think you can just force upgrade React to v19 RC and it will solve this. Next.js 14 is bundled with React 19 but they install React 18 as the dependency. So when you run tests it will use React 18, which does not have the Weird it is, but instead of messing with mocks etc, just upgrade to React 19 and you should be golden. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
When testing components that use React Cache, the following error is returned:
Anyone know the resolution to this? I tried stubbing it but that says the property doesn't exist as well.
I'm just presuming none of this React canary stuff has been tested? There are no docs on any of the RSC or other canary items anyway, which is slightly annoying considering this is apparently stable, not sure I can agree if this is not testable...
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions