Skip to content

renderHook() results in: Warning: ReactDOM.render is no longer supported in React 18. #826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alxlacayo opened this issue Apr 16, 2022 · 4 comments
Labels
question Further information is requested

Comments

@alxlacayo
Copy link

I'm getting the React 18 createRoot warning when using renderHook. I've updated my app to react 18 already and have the latest testing library installed. Does anyone know the solution to this?

import { renderHook } from "@testing-library/react-hooks";

test("should autoconnect", async () => { 
    const { result, waitForNextUpdate } = renderHook(() => useConnectionService(ethereumProviders, storageService));
    await waitForNextUpdate();
    expect(result.current[0].account).not.toBeNull();
});
Results in     console.error
      Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. Learn more: https://reactjs.org/link/switch-to-createroot
@alxlacayo alxlacayo added the question Further information is requested label Apr 16, 2022
@mpeyper
Copy link
Member

mpeyper commented Apr 16, 2022

Please see the note in the README about react 18 support.

The RTL PR has been merged now, so if you’ve updated your app and other tests to React 18, you should be ready to migrate your renderHook imports to @testing-library/react now too.

@scssyworks
Copy link

I am still getting the warning An update to TestComponent inside a test was not wrapped in act(...)

I tried wrapping renderHook inside an act but in TypeScript I get following error:

let hook: RenderHookResult<any,any>;
act(() => {
  hook = renderHook(myHook);
});
const { result } = hook; // Here I get typescript error saying: "Trying to use variable before it's assigned"

@LoveMeWithoutAll
Copy link

It would be worked.

import { renderHook, waitFor } from '@testing-library/react';

it('test', async () => {
    jest
      .spyOn(adminService, 'getNotice')
      .mockImplementation(() => Promise.resolve({} as NoticeResponse));
    const { result } = renderHook(() => useGetNotice('123'), { wrapper });
    await waitFor(() =>
      expect(result.current.data).toStrictEqual({} as NoticeResponse),
    );
  });

@seyhak
Copy link

seyhak commented Dec 9, 2023

Please see the note in the README about react 18 support.

The RTL PR has been merged now, so if you’ve updated your app and other tests to React 18, you should be ready to migrate your renderHook imports to @testing-library/react now too.

Yeah, I changed import { renderHook, act } from '@testing-library/react-hooks';
to import { renderHook, act } from '@testing-library/react';
and the error disappeared

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants