From 1416ccdb0e4635ef4af677ff8d6350169ad5eb6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Thu, 8 Jan 2026 23:12:40 +0100 Subject: [PATCH 1/3] chore: more tests --- src/__tests__/wait-for.test.tsx | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/__tests__/wait-for.test.tsx b/src/__tests__/wait-for.test.tsx index b9235b3e9..cd4ee2e0a 100644 --- a/src/__tests__/wait-for.test.tsx +++ b/src/__tests__/wait-for.test.tsx @@ -316,3 +316,34 @@ test('waitFor throws if expectation is not a function', async () => { waitFor('not a function'), ).rejects.toThrowErrorMatchingInlineSnapshot(`"Received \`expectation\` arg must be a function"`); }); + +test.each([false, true])( + 'waitFor throws clear error when switching from fake timers to real timers (legacyFakeTimers = %s)', + async (legacyFakeTimers) => { + jest.useFakeTimers({ legacyFakeTimers }); + + const waitForPromise = waitFor(() => { + // Switch to real timers during waitFor - this should trigger an error + jest.useRealTimers(); + throw new Error('test'); + }); + + await expect(waitForPromise).rejects.toThrow( + 'Changed from using fake timers to real timers while using waitFor', + ); + }, +); + +test('waitFor throws clear error when switching from real timers to fake timers', async () => { + jest.useRealTimers(); + + const waitForPromise = waitFor(() => { + // Switch to fake timers during waitFor - this should trigger an error + jest.useFakeTimers(); + throw new Error('test'); + }); + + await expect(waitForPromise).rejects.toThrow( + 'Changed from using real timers to fake timers while using waitFor', + ); +}); From b16334cfd7dda45977afae989d86ad2518e317aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Thu, 8 Jan 2026 23:16:53 +0100 Subject: [PATCH 2/3] . --- src/__tests__/act.test.tsx | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/__tests__/act.test.tsx b/src/__tests__/act.test.tsx index 08944e994..30896d909 100644 --- a/src/__tests__/act.test.tsx +++ b/src/__tests__/act.test.tsx @@ -2,6 +2,7 @@ import * as React from 'react'; import { Text } from 'react-native'; import { act, fireEvent, render, screen } from '..'; +import { getIsReactActEnvironment } from '../act'; type UseEffectProps = { callback(): void }; const UseEffect = ({ callback }: UseEffectProps) => { @@ -48,3 +49,36 @@ test('should be able to await act', async () => { test('should be able to await act when promise rejects', async () => { await expect(act(() => Promise.reject('error'))).rejects.toBe('error'); }); + +test('should restore act environment when callback throws synchronously', async () => { + const previousEnvironment = getIsReactActEnvironment(); + + const testError = new Error('Synchronous error in act'); + + try { + await act(() => { + throw testError; + }); + // Should not reach here + expect(true).toBe(false); + } catch (error) { + expect(error).toBe(testError); + } + + // Verify the act environment was restored even after error + expect(getIsReactActEnvironment()).toBe(previousEnvironment); +}); + +test('should restore act environment when callback returns non-promise value', async () => { + const previousEnvironment = getIsReactActEnvironment(); + + // Call act with a callback that returns a non-promise value + // This tests the else branch in withGlobalActEnvironment + const result = await act(() => { + return 42; + }); + + expect(result).toBe(42); + // Verify the act environment was restored + expect(getIsReactActEnvironment()).toBe(previousEnvironment); +}); From 511f07b8d67f046000f82ed64878c89b5811fa73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Jastrze=CC=A8bski?= Date: Fri, 9 Jan 2026 23:01:29 +0100 Subject: [PATCH 3/3] . --- src/__tests__/act.test.tsx | 22 +++++++++------------- src/__tests__/wait-for.test.tsx | 16 ++++++++-------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/__tests__/act.test.tsx b/src/__tests__/act.test.tsx index 30896d909..dfe20e3e5 100644 --- a/src/__tests__/act.test.tsx +++ b/src/__tests__/act.test.tsx @@ -52,32 +52,28 @@ test('should be able to await act when promise rejects', async () => { test('should restore act environment when callback throws synchronously', async () => { const previousEnvironment = getIsReactActEnvironment(); - + const testError = new Error('Synchronous error in act'); - - try { - await act(() => { + + await expect( + act(() => { throw testError; - }); - // Should not reach here - expect(true).toBe(false); - } catch (error) { - expect(error).toBe(testError); - } - + }), + ).rejects.toBe(testError); + // Verify the act environment was restored even after error expect(getIsReactActEnvironment()).toBe(previousEnvironment); }); test('should restore act environment when callback returns non-promise value', async () => { const previousEnvironment = getIsReactActEnvironment(); - + // Call act with a callback that returns a non-promise value // This tests the else branch in withGlobalActEnvironment const result = await act(() => { return 42; }); - + expect(result).toBe(42); // Verify the act environment was restored expect(getIsReactActEnvironment()).toBe(previousEnvironment); diff --git a/src/__tests__/wait-for.test.tsx b/src/__tests__/wait-for.test.tsx index cd4ee2e0a..88c8429d3 100644 --- a/src/__tests__/wait-for.test.tsx +++ b/src/__tests__/wait-for.test.tsx @@ -337,13 +337,13 @@ test.each([false, true])( test('waitFor throws clear error when switching from real timers to fake timers', async () => { jest.useRealTimers(); - const waitForPromise = waitFor(() => { - // Switch to fake timers during waitFor - this should trigger an error - jest.useFakeTimers(); - throw new Error('test'); - }); + const waitForPromise = waitFor(() => { + // Switch to fake timers during waitFor - this should trigger an error + jest.useFakeTimers(); + throw new Error('test'); + }); - await expect(waitForPromise).rejects.toThrow( - 'Changed from using real timers to fake timers while using waitFor', - ); + await expect(waitForPromise).rejects.toThrow( + 'Changed from using real timers to fake timers while using waitFor', + ); });