Skip to content

Is Rtl.fireEvent() compatible with react hooks testing library? #141

@gzaripov

Description

@gzaripov

I have just updated to new versions of react and testing-library and now I see this warning in my hook's test file:

 Warning: It looks like you're using the wrong act() around your test interactions.
      Be sure to use the matching version of act() corresponding to your renderer:

      // for react-dom:
      import {act} from 'react-dom/test-utils';
      //...
      act(() => ...);

      // for react-test-renderer:
      import TestRenderer from 'react-test-renderer';
      const {act} = TestRenderer;
      //...
      act(() => ...);
          in TestHook
          in Suspense

Here is code of my test:

import { fireEvent } from '@testing-library/react';
import { renderHook, act } from '@testing-library/react-hooks';

// ...

it('should update return value on hover', () => {
    jest.useFakeTimers();

    const ref = createRefMock();
    const { result } = renderHook(() => useHover(ref));

    expect(result.current).toBe(false);

    fireEvent.mouseOver(ref.current);

    act(() => {
        jest.runOnlyPendingTimers();
    });

    expect(result.current).toBe(true);

    fireEvent.mouseOut(ref.current);

    expect(result.current).toBe(false);
});

Warning appears on line fireEvent.mouseOut(ref.current);

And if I change it to

act(() => {
     fireEvent.mouseOut(ref.current);
});

everything is working fine.

But is still strange that I don't have any problems with fireEvent.mouseOver.

Do I need to wrap every fireEvent to act in my code?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions