luisanton-io / recoil-nexus

MIT License
163 stars 13 forks source link

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

Open Kamahl19 opened 1 year ago

Kamahl19 commented 1 year ago

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
luisanton-io commented 1 year ago

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?

Kamahl19 commented 1 year ago

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

luisanton-io commented 1 year ago

Could be similar to this?

Kamahl19 commented 1 year ago

@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

RobinClowers commented 12 months ago

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.