Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/__tests__/act.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -48,3 +49,32 @@ 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');

await expect(
act(() => {
throw 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);
});
31 changes: 31 additions & 0 deletions src/__tests__/wait-for.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
});