Skip to content

React.act is not a function in vitest #1392

Closed
@punkpeye

Description

@punkpeye

Here is my test code:

// @vitest-environment jsdom

import { useChatSessionFocus } from './useChatSessionFocus';
import { act, renderHook } from '@testing-library/react';
import { expect, it } from 'vitest';

it('focusMessages changes the focused component to messages', () => {
  const { result } = renderHook(() => useChatSessionFocus(true));

  expect(result.current.focusedComponent).toBe('message-form');

  act(() => {
    result.current.focusMessages();
  });

  expect(result.current.focusedComponent).toBe('messages');
});

It is failing in CI with:

   × returns the expected initial state 10ms
     → React.act is not a function

There is also a warning:

`ReactDOMTestUtils.act` is deprecated in favor of `React.act`. Import `act` from `react` instead of `react-dom/test-utils`. See https://react.dev/warnings/react-dom-test-utils for more info.

The confusing part is that the exact same test setup works perfectly fine locally.

I cannot figure out why vitest or react-testing-library is behaving differently in CI.

I already considered the possibility of version mismatch, but adding overrides didn't help:

"overrides": {
  "zod": "3.24.2",
  "react": "19.1.0",
  "react-dom": "19.1.0"
},

Would appreciate any guidance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions