facebookexperimental / Recoil

Recoil is an experimental state management library for React apps. It provides several capabilities that are difficult to achieve with React alone, while being compatible with the newest features of React.
https://recoiljs.org/
MIT License
19.59k stars 1.18k forks source link

Issue testing `recoil-sync` with `vitest` #2282

Open patwalravi opened 1 year ago

patwalravi commented 1 year ago

Hello,

I am facing an issue with testing recoil-sync with vitest.

Versions:

Below is a basic simple test file using recoil-sync:

import { render, screen } from '@testing-library/react';
import React from 'react';
import { RecoilRoot } from 'recoil';
import Sync from 'recoil-sync';

const TestComponent = () => {
  return (
    <RecoilRoot>
      <React.Suspense fallback={null}>
        <Sync.RecoilSync
          storeKey={'RecoilSyncWithUrlContext'}
          read={() => {
            return "test";
          }}
        >
          <h1>test</h1>
        </Sync.RecoilSync>
      </React.Suspense>
    </RecoilRoot>
  );
};
describe('Testing ReoilSync', () => {
  it('should render something', async () => {
    render(<TestComponent />);
    expect(
      await screen.findByText("test")
    ).toBeInTheDocument();
  });
});

Getting the following error on running test (even though the RecoilState wraps the element):

Error: This component must be used inside a <RecoilRoot> component.
 ❯ err node_modules/recoil/cjs/index.js:22:17
 ❯ Object.notInAContext node_modules/recoil/cjs/index.js:4181:9
 ❯ node_modules/recoil/cjs/index.js:3985:90
 ❯ memoizedFn node_modules/recoil/cjs/index.js:3620:17
 ❯ cloneSnapshot node_modules/recoil/cjs/index.js:3993:20
 ❯ node_modules/recoil/cjs/index.js:5760:52
 ❯ mountState node_modules/react-dom/cjs/react-dom.development.js:16986:20
 ❯ Object.useState node_modules/react-dom/cjs/react-dom.development.js:17699:16
 ❯ useState node_modules/react/cjs/react.development.js:1622:21
 ❯ useRecoilSnapshot node_modules/recoil/cjs/index.js:5760:35

Steps to produce:

Any thoughts on what might be causing the issue and how can I resolve it.

Thanks a lot :)

ra-ch-b commented 7 months ago

I'm seeing the same error with recoil 0.7.7, recoil-sync 0.2.0, vitest 1.2.2

@patwalravi did you find a solution or workaround for the issue?

jsnbuchanan commented 3 months ago

I'm still seeing this as well.