Skip to content

Bug: Support use in act testing API #25523 #32212

@Mohammed-Karim226

Description

@Mohammed-Karim226

React version:
Main branch (as of October 21, 2022, when PR #25523 was merged).

Steps To Reproduce

  1. Write a test that uses the use hook inside a component.

  2. Wrap the test in act but do not await the thenable returned by act.

  3. Observe that the test may fail or behave inconsistently due to improper task scheduling.

Link to code example:
javascript

import { act, render } from '@testing-library/react';
import React, { use } from 'react';

async function fetchData() {
return Promise.resolve('data');
}

function Component() {
const data = use(fetchData());
return

{data}
;
}

test('use hook in act', async () => {
await act(async () => {
render();
});
});

The current behavior

If the thenable returned by act is not awaited, tasks scheduled by use may not be flushed correctly, leading to inconsistent test behavior.

No warning is shown unless use is called, which can make debugging difficult.

The expected behavior

The act API should fully support the use hook, ensuring tasks are properly scheduled and flushed.

A warning should be shown if the thenable returned by act is not awaited, helping developers identify potential issues.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions