Skip to content

Commit 7d0cc8a

Browse files
committed
refactor: unsafe_act
1 parent 88615b4 commit 7d0cc8a

File tree

7 files changed

+6
-104
lines changed

7 files changed

+6
-104
lines changed

codemods/v14-async-functions/tests/fixtures/skip-variants/expected.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
render,
33
act,
44
renderHook,
5-
unsafe_act,
65
} from '@testing-library/react-native';
76

87
test('skips unsafe variants', async () => {
@@ -12,10 +11,6 @@ test('skips unsafe variants', async () => {
1211
// Should be transformed
1312
});
1413

15-
unsafe_act(() => {
16-
// Should NOT be transformed
17-
});
18-
1914
const { result } = await renderHook(() => ({ value: 42 }));
2015

2116
await render(<MyComponent />);

codemods/v14-async-functions/tests/fixtures/skip-variants/input.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
render,
33
act,
44
renderHook,
5-
unsafe_act,
65
renderAsync,
76
} from '@testing-library/react-native';
87

@@ -13,10 +12,6 @@ test('skips unsafe variants', async () => {
1312
// Should be transformed
1413
});
1514

16-
unsafe_act(() => {
17-
// Should NOT be transformed
18-
});
19-
2015
const { result } = renderHook(() => ({ value: 42 }));
2116

2217
await renderAsync(<MyComponent />);

src/__tests__/__snapshots__/unsafe-render-sync.test.tsx.snap

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/act.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ function withGlobalActEnvironment(actImplementation: ReactAct) {
6666
};
6767
}
6868

69-
export const unsafe_act = withGlobalActEnvironment(reactAct) as ReactAct;
69+
const _act = withGlobalActEnvironment(reactAct) as ReactAct;
7070

7171
export function act<T>(callback: () => T | Promise<T>): Promise<T> {
72-
return unsafe_act(async () => await callback());
72+
return _act(async () => await callback());
7373
}
7474

7575
export { getIsReactActEnvironment, setIsReactActEnvironment as setReactActEnvironment };

src/pure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { act, unsafe_act } from './act';
1+
export { act } from './act';
22
export { cleanup } from './cleanup';
33
export { fireEvent } from './fire-event';
44
export { render } from './render';

website/docs/14.x/docs/api/misc/other.mdx

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,6 @@ it('should update state', async () => {
5252

5353
Consult our [Understanding Act function](docs/advanced/understanding-act.md) document for more understanding of its intricacies.
5454

55-
## `unsafe_act`
56-
57-
```ts
58-
function unsafe_act<T>(callback: () => T | Promise<T>): T | Thenable<T>;
59-
```
60-
61-
**⚠️ Deprecated**: This function is provided for migration purposes only. Use async `act` instead.
62-
63-
The synchronous version of `act` that maintains the same behavior as v13. It returns immediately for sync callbacks or a thenable for async callbacks. This is not recommended for new code as it doesn't work well with React 19's async features.
64-
65-
```ts
66-
// Deprecated - use act instead
67-
import { unsafe_act } from '@testing-library/react-native';
68-
69-
it('should update state', () => {
70-
unsafe_act(() => {
71-
setState('new value');
72-
});
73-
expect(state).toBe('new value');
74-
});
75-
```
76-
7755
## `cleanup`
7856

7957
```ts

website/docs/14.x/docs/migration/v14.mdx

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -416,13 +416,12 @@ If you were relying on the previous behavior where strings could be rendered out
416416
417417
### `act` is now async by default
418418
419-
In v14, `act` is now async by default and always returns a Promise. The previous synchronous `act` function has been renamed to `unsafe_act`. This change makes it compatible with React 19, React Suspense, and `React.use()`.
419+
In v14, `act` is now async by default and always returns a Promise. This change makes it compatible with React 19, React Suspense, and `React.use()`.
420420
421421
**What changed:**
422422
423423
- `act` now always returns a `Promise<T>` instead of `T | Thenable<T>`
424-
- `unsafe_act` is available for the old behavior (direct React.act wrapper)
425-
- Both `act` and `unsafe_act` are now named exports (no default export)
424+
- `act` is now a named export (no default export)
426425
427426
**Before (v13):**
428427
@@ -454,35 +453,9 @@ it('should update state', async () => {
454453
});
455454
```
456455
457-
#### Migration path
458-
459-
To ease migration, we provide `unsafe_act` which maintains the same behavior as v13. This allows you to migrate gradually.
460-
461-
##### `unsafe_act` {#unsafe-act}
462-
463-
```ts
464-
function unsafe_act<T>(callback: () => T | Promise<T>): T | Thenable<T>;
465-
```
466-
467-
**⚠️ Deprecated**: This function is provided for migration purposes only. Use async `act` instead.
468-
469-
The synchronous version of `act` that returns immediately for sync callbacks or a thenable for async callbacks. This maintains backward compatibility with v13 tests but is not recommended for new code.
470-
471-
```ts
472-
// Old v13 code (still works but deprecated)
473-
import { unsafe_act } from '@testing-library/react-native';
474-
475-
it('should update state', () => {
476-
unsafe_act(() => {
477-
setState('new value');
478-
});
479-
expect(state).toBe('new value');
480-
});
481-
```
482-
483456
#### Step-by-step migration guide
484457
485-
To migrate from `unsafe_act` to `act`:
458+
To migrate from v13 `act` to v14 `act`:
486459
487460
##### 1. Update import statement
488461

0 commit comments

Comments
 (0)