Skip to content

Commit 20ef3eb

Browse files
committed
more tweaking
1 parent c124751 commit 20ef3eb

10 files changed

+43
-42
lines changed

src/__tests__/renderHookToSnapshotStream.test.tsx

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import {EventEmitter} from 'node:events'
44
import {test, expect} from '@jest/globals'
55
import {renderHookToSnapshotStream} from '@testing-library/react-render-stream'
66
import * as React from 'react'
7-
import {withDisabledActWarnings} from '../__testHelpers__/withDisabledActWarnings.js'
8-
9-
// @ts-expect-error this is not defined anywhere
10-
globalThis.IS_REACT_ACT_ENVIRONMENT = false
117

128
const testEvents = new EventEmitter<{
139
rerenderWithValue: [unknown]
@@ -19,7 +15,7 @@ function useRerenderEvents(initialValue: unknown) {
1915
onChange => {
2016
const cb = (value: unknown) => {
2117
lastValueRef.current = value
22-
withDisabledActWarnings(onChange)
18+
onChange()
2319
}
2420
testEvents.addListener('rerenderWithValue', cb)
2521
return () => {

src/__tests__/renderToRenderStream.test.tsx

-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import {
77
import {userEvent as baseUserEvent} from '@testing-library/user-event'
88
import * as React from 'react'
99

10-
// @ts-expect-error this is not defined anywhere
11-
globalThis.IS_REACT_ACT_ENVIRONMENT = false
12-
1310
const userEvent = userEventWithoutAct(baseUserEvent)
1411

1512
function CounterForm({

src/disableActEnvironment.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const dispose: typeof Symbol.dispose =
2+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
3+
Symbol.dispose ?? Symbol.for('nodejs.dispose')
4+
5+
/**
6+
* Temporarily disable act warnings.
7+
*
8+
* https://github.com/reactwg/react-18/discussions/102
9+
*/
10+
export function disableActEnvironment(): {cleanup: () => void} & Disposable {
11+
const anyThis = globalThis as any as {IS_REACT_ACT_ENVIRONMENT?: boolean}
12+
const prevActEnv = anyThis.IS_REACT_ACT_ENVIRONMENT
13+
anyThis.IS_REACT_ACT_ENVIRONMENT = false
14+
15+
function cleanup() {
16+
anyThis.IS_REACT_ACT_ENVIRONMENT = prevActEnv
17+
}
18+
return {
19+
cleanup,
20+
[dispose]: cleanup,
21+
}
22+
}

src/expect/__tests__/renderStreamMatchers.test.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
} from '@testing-library/react-render-stream'
88
import * as React from 'react'
99
import {getExpectErrorMessage} from '../../__testHelpers__/getCleanedErrorMessage.js'
10-
import {withDisabledActWarnings} from '../../__testHelpers__/withDisabledActWarnings.js'
1110

1211
const testEvents = new EventEmitter<{
1312
rerender: []
@@ -16,7 +15,7 @@ const testEvents = new EventEmitter<{
1615
function useRerender() {
1716
const [, rerender] = React.useReducer(c => c + 1, 0)
1817
React.useEffect(() => {
19-
const cb = () => void withDisabledActWarnings(rerender)
18+
const cb = () => void rerender()
2019

2120
testEvents.addListener('rerender', cb)
2221
return () => {

src/pure.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ export type {SnapshotStream} from './renderHookToSnapshotStream.js'
1919

2020
export type {Assertable} from './assertable.js'
2121

22-
export {renderWithoutAct, cleanup} from './renderStream/renderWithoutAct.js'
22+
export {renderWithoutAct, cleanup} from './renderWithoutAct.js'
2323
export {userEventWithoutAct} from './useWithoutAct.js'
24+
export {disableActEnvironment} from './disableActEnvironment.js'
25+
export {withDisabledActEnvironment} from './withDisabledActEnvironment.js'

src/renderHookToSnapshotStream.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {RenderHookOptions} from '@testing-library/react'
1+
import {type RenderHookOptions} from '@testing-library/react'
22
import React from 'rehackt'
33
import {createRenderStream} from './renderStream/createRenderStream.js'
44
import {type NextRenderOptions} from './renderStream/createRenderStream.js'

src/renderStream/createRenderStream.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import * as React from 'rehackt'
22

33
import {RenderOptions} from '@testing-library/react'
44
import {Assertable, markAssertable} from '../assertable.js'
5+
import {disableActEnvironment} from '../disableActEnvironment.js'
6+
import {renderWithoutAct, RenderWithoutActAsync} from '../renderWithoutAct.js'
57
import {RenderInstance, type Render, type BaseRender} from './Render.js'
68
import {type RenderStreamContextValue} from './context.js'
79
import {RenderStreamContextProvider} from './context.js'
8-
import {disableActWarnings} from './disableActWarnings.js'
910
import {syncQueries, type Queries, type SyncQueries} from './syncQueries.js'
10-
import {renderWithoutAct, RenderWithoutActAsync} from './renderWithoutAct.js'
1111

1212
export type ValidSnapshot =
1313
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
@@ -299,7 +299,7 @@ export function createRenderStream<
299299
// In many cases we do not control the resolution of the suspended
300300
// promise which results in noisy tests when the profiler due to
301301
// repeated act warnings.
302-
const disabledActWarnings = disableActWarnings()
302+
const disabledAct = disableActEnvironment()
303303

304304
let error: unknown
305305

@@ -317,7 +317,7 @@ export function createRenderStream<
317317
if (!(error && error instanceof WaitForRenderTimeoutError)) {
318318
iteratorPosition++
319319
}
320-
disabledActWarnings.cleanup()
320+
disabledAct.cleanup()
321321
}
322322
}, stream),
323323
getCurrentRender() {

src/renderStream/disableActWarnings.ts

-16
This file was deleted.

src/renderStream/renderWithoutAct.tsx renamed to src/renderWithoutAct.tsx

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import {
55
Queries,
66
type RenderOptions,
77
type RenderResult,
8-
} from '@testing-library/react'
8+
} from '@testing-library/react/pure.js'
99
import React from 'react'
10-
import {SyncQueries} from './syncQueries.js'
10+
import {SyncQueries} from './renderStream/syncQueries.js'
11+
import {withDisabledActEnvironment} from './withDisabledActEnvironment.js'
1112

1213
// Ideally we'd just use a WeakMap where containers are keys and roots are values.
1314
// We use two variables so that we can bail out in constant time when we render with a new container (most common use case)
@@ -32,8 +33,6 @@ function renderRoot(
3233
root: ReturnType<typeof createConcurrentRoot>
3334
},
3435
): RenderResult<Queries, any, any> {
35-
// @ts-expect-error this is not defined anywhere
36-
globalThis.IS_REACT_ACT_ENVIRONMENT = false
3736
root.render(
3837
WrapperComponent ? React.createElement(WrapperComponent, null, ui) : ui,
3938
)
@@ -164,14 +163,16 @@ export function renderWithoutAct(
164163
}
165164

166165
function createConcurrentRoot(container: ReactDOMClient.Container) {
167-
const root = ReactDOMClient.createRoot(container)
166+
const root = withDisabledActEnvironment(() =>
167+
ReactDOMClient.createRoot(container),
168+
)
168169

169170
return {
170171
render(element: React.ReactNode) {
171-
root.render(element)
172+
withDisabledActEnvironment(() => root.render(element))
172173
},
173174
unmount() {
174-
root.unmount()
175+
withDisabledActEnvironment(() => root.unmount())
175176
},
176177
}
177178
}

src/__testHelpers__/withDisabledActWarnings.ts renamed to src/withDisabledActEnvironment.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {disableActWarnings} from '../renderStream/disableActWarnings.js'
1+
import {disableActEnvironment} from './disableActEnvironment.js'
22

3-
export function withDisabledActWarnings<T>(cb: () => T): T {
4-
const disabledActWarnings = disableActWarnings()
3+
export function withDisabledActEnvironment<T>(cb: () => T): T {
4+
const disabledActWarnings = disableActEnvironment()
55
let result: T
66
try {
77
result = cb()

0 commit comments

Comments
 (0)