recoil-nexus icon indicating copy to clipboard operation
recoil-nexus copied to clipboard

Vitest >= 0.32 "This component must be used inside a <RecoilRoot> component."

Open Kamahl19 opened this issue 9 months ago • 5 comments

After upgrading from Vitest 0.31.4 to 0.32.0 I am getting

Error: This component must be used inside a <RecoilRoot> component.
    at err (/node_modules/recoil/cjs/index.js:22:17)
    at Object.notInAContext (/node_modules/recoil/cjs/index.js:4182:9)
    at /node_modules/recoil/cjs/index.js:3975:90
    at memoizedFn (/node_modules/recoil/cjs/index.js:3615:17)
    at cloneSnapshot (/node_modules/recoil/cjs/index.js:3981:20)
    at Object.snapshot (/node_modules/recoil/cjs/index.js:6109:26)
    at Object.get (/node_modules/recoil/cjs/index.js:2451:39)
    at /node_modules/recoil-nexus/src/RecoilNexus.tsx:15:77
    at /node_modules/recoil/cjs/index.js:6114:22
    at /node_modules/recoil/cjs/index.js:3477:7

Kamahl19 avatar Sep 18 '23 15:09 Kamahl19

Hi, it looks like the error is happening on useRecoilCallback, apparently when first invoked it can't find the context. Which versions of Recoil and Recoil Nexus are you using?

luisanton-io avatar Sep 21 '23 14:09 luisanton-io

@luisanton-io Thanks for getting back to me. I use the latest recoil and latest nexus

Kamahl19 avatar Sep 21 '23 14:09 Kamahl19

Could be similar to this?

luisanton-io avatar Sep 21 '23 14:09 luisanton-io

@luisanton-io Here is the codebase I am using it in https://github.com/Kamahl19/react-starter . I am wrapping the whole application in RecoilRoot in https://github.com/Kamahl19/react-starter/blob/c568c273875fad3a5a8058f2c4f396cb041293d8/src/app/providers/Recoil.tsx#L17 which is used in https://github.com/Kamahl19/react-starter/blob/c568c273875fad3a5a8058f2c4f396cb041293d8/src/app/Root.tsx#L17 and https://github.com/Kamahl19/react-starter/blob/c568c273875fad3a5a8058f2c4f396cb041293d8/src/tests/utils.tsx#L34

In this commit I have updated to latest Vitest but had to stop using Nexus and partly copy-paste its functionality https://github.com/Kamahl19/react-starter/commit/c568c273875fad3a5a8058f2c4f396cb041293d8

Kamahl19 avatar Sep 21 '23 14:09 Kamahl19

Weird, I also ran into this when migrating from jest to vitest. I'm not at all sure what would cause this, I have a recoil root in my test, and it worked fine under jest. The only thing I can think is that I'm also migrating my package to ESM at the same time, probably something about loading this package (which appears to be commonjs) in an ESM package is somehow broken.

RobinClowers avatar Nov 09 '23 20:11 RobinClowers