Skip to content

Commit bb01b37

Browse files
committed
Use IS_REACT_ACT_ENVIRONMENT to disable warnings
If `IS_REACT_ACT_ENVIRONMENT` is set to `false`, we will suppress any `act` warnings. Otherwise, the behavior of `act` is the same as in React 17: if `jest` is defined, it warns. In concurrent mode, the plan is to remove the `jest` check and only warn if `IS_REACT_ACT_ENVIRONMENT` is true. I have not implemented that part yet.
1 parent 34c3c35 commit bb01b37

File tree

12 files changed

+90
-22
lines changed

12 files changed

+90
-22
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,5 +279,6 @@ module.exports = {
279279
__VARIANT__: true,
280280
gate: true,
281281
trustedTypes: true,
282+
IS_REACT_ACT_ENVIRONMENT: true,
282283
},
283284
};

packages/jest-react/src/internalAct.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ import type {Thenable} from 'shared/ReactTypes';
1818

1919
import * as Scheduler from 'scheduler/unstable_mock';
2020

21-
import ReactSharedInternals from 'shared/ReactSharedInternals';
2221
import enqueueTask from 'shared/enqueueTask';
23-
const {ReactCurrentActQueue} = ReactSharedInternals;
2422

2523
let actingUpdatesScopeDepth = 0;
2624

@@ -37,15 +35,18 @@ export function act(scope: () => Thenable<mixed> | void) {
3735
);
3836
}
3937

38+
const previousIsActEnvironment = global.IS_REACT_ACT_ENVIRONMENT;
4039
const previousActingUpdatesScopeDepth = actingUpdatesScopeDepth;
4140
actingUpdatesScopeDepth++;
4241
if (__DEV__ && actingUpdatesScopeDepth === 1) {
43-
ReactCurrentActQueue.disableActWarning = true;
42+
// Because this is not the "real" `act`, we set this to `false` so React
43+
// knows not to fire `act` warnings.
44+
global.IS_REACT_ACT_ENVIRONMENT = false;
4445
}
4546

4647
const unwind = () => {
4748
if (__DEV__ && actingUpdatesScopeDepth === 1) {
48-
ReactCurrentActQueue.disableActWarning = false;
49+
global.IS_REACT_ACT_ENVIRONMENT = previousIsActEnvironment;
4950
}
5051
actingUpdatesScopeDepth--;
5152

packages/react-dom/src/__tests__/ReactTestUtilsAct-test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,40 @@ function runActTests(label, render, unmount, rerender) {
273273
]);
274274
});
275275

276+
// @gate __DEV__
277+
it('does not warn if IS_REACT_ACT_ENVIRONMENT is set to false', () => {
278+
let setState;
279+
function App() {
280+
const [state, _setState] = React.useState(0);
281+
setState = _setState;
282+
return state;
283+
}
284+
285+
act(() => {
286+
render(<App />, container);
287+
});
288+
289+
// First show that it does warn
290+
expect(() => setState(1)).toErrorDev(
291+
'An update to App inside a test was not wrapped in act(...)',
292+
);
293+
294+
// Now do the same thing again, but disable with the environment flag
295+
const prevIsActEnvironment = process.env.IS_REACT_ACT_ENVIRONMENT;
296+
global.IS_REACT_ACT_ENVIRONMENT = false;
297+
try {
298+
setState(2);
299+
} finally {
300+
global.IS_REACT_ACT_ENVIRONMENT = prevIsActEnvironment;
301+
}
302+
303+
// When the flag is restored to its previous value, it should start
304+
// warning again. This shows that React reads the flag each time.
305+
expect(() => setState(3)).toErrorDev(
306+
'An update to App inside a test was not wrapped in act(...)',
307+
);
308+
});
309+
276310
describe('fake timers', () => {
277311
beforeEach(() => {
278312
jest.useFakeTimers();

packages/react-reconciler/src/ReactFiberAct.new.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@
1010
import type {Fiber} from './ReactFiber.new';
1111
import {warnsIfNotActing} from './ReactFiberHostConfig';
1212

13-
import ReactSharedInternals from 'shared/ReactSharedInternals';
14-
15-
const {ReactCurrentActQueue} = ReactSharedInternals;
16-
1713
export function isActEnvironment(fiber: Fiber) {
18-
const disableActWarning = ReactCurrentActQueue.disableActWarning;
14+
const isReactActEnvironmentGlobal =
15+
// $FlowExpectedError – Flow doesn't know about IS_REACT_ACT_ENVIRONMENT global
16+
typeof IS_REACT_ACT_ENVIRONMENT !== 'undefined'
17+
? IS_REACT_ACT_ENVIRONMENT
18+
: undefined;
19+
20+
// TODO: Only check `jest` in legacy mode. In concurrent mode, this heuristic
21+
// is repalced by IS_REACT_ACT_ENVIRONMENT.
1922
// $FlowExpectedError - Flow doesn't know about jest
2023
const jestIsDefined = typeof jest !== 'undefined';
21-
return warnsIfNotActing && jestIsDefined && !disableActWarning;
24+
return (
25+
warnsIfNotActing &&
26+
jestIsDefined &&
27+
// Legacy mode assumes an act environment whenever `jest` is defined, but
28+
// you can still turn off spurious warnings by setting
29+
// IS_REACT_ACT_ENVIRONMENT explicitly to false.
30+
isReactActEnvironmentGlobal !== false
31+
);
2232
}

packages/react-reconciler/src/ReactFiberAct.old.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@
1010
import type {Fiber} from './ReactFiber.old';
1111
import {warnsIfNotActing} from './ReactFiberHostConfig';
1212

13-
import ReactSharedInternals from 'shared/ReactSharedInternals';
14-
15-
const {ReactCurrentActQueue} = ReactSharedInternals;
16-
1713
export function isActEnvironment(fiber: Fiber) {
18-
const disableActWarning = ReactCurrentActQueue.disableActWarning;
14+
const isReactActEnvironmentGlobal =
15+
// $FlowExpectedError – Flow doesn't know about IS_REACT_ACT_ENVIRONMENT global
16+
typeof IS_REACT_ACT_ENVIRONMENT !== 'undefined'
17+
? IS_REACT_ACT_ENVIRONMENT
18+
: undefined;
19+
20+
// TODO: Only check `jest` in legacy mode. In concurrent mode, this heuristic
21+
// is repalced by IS_REACT_ACT_ENVIRONMENT.
1922
// $FlowExpectedError - Flow doesn't know about jest
2023
const jestIsDefined = typeof jest !== 'undefined';
21-
return warnsIfNotActing && jestIsDefined && !disableActWarning;
24+
return (
25+
warnsIfNotActing &&
26+
jestIsDefined &&
27+
// Legacy mode assumes an act environment whenever `jest` is defined, but
28+
// you can still turn off spurious warnings by setting
29+
// IS_REACT_ACT_ENVIRONMENT explicitly to false.
30+
isReactActEnvironmentGlobal !== false
31+
);
2232
}

packages/react/src/ReactCurrentActQueue.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ type RendererTask = boolean => RendererTask | null;
1111

1212
const ReactCurrentActQueue = {
1313
current: (null: null | Array<RendererTask>),
14-
// Our internal tests use a custom implementation of `act` that works by
15-
// mocking the Scheduler package. Use this field to disable the `act` warning.
16-
// TODO: Maybe the warning should be disabled by default, and then turned
17-
// on at the testing frameworks layer? Instead of what we do now, which
18-
// is check if a `jest` global is defined.
19-
disableActWarning: (false: boolean),
2014

2115
// Used to reproduce behavior of `batchedUpdates` in legacy mode.
2216
isBatchingLegacy: false,

scripts/rollup/validate/eslintrc.cjs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ module.exports = {
4242
// jest
4343
expect: true,
4444
jest: true,
45+
46+
// act
47+
IS_REACT_ACT_ENVIRONMENT: true,
4548
},
4649
parserOptions: {
4750
ecmaVersion: 5,

scripts/rollup/validate/eslintrc.cjs2015.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ module.exports = {
4242
// jest
4343
expect: true,
4444
jest: true,
45+
46+
// act
47+
IS_REACT_ACT_ENVIRONMENT: true,
4548
},
4649
parserOptions: {
4750
ecmaVersion: 2015,

scripts/rollup/validate/eslintrc.esm.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ module.exports = {
4242
// jest
4343
expect: true,
4444
jest: true,
45+
46+
// act
47+
IS_REACT_ACT_ENVIRONMENT: true,
4548
},
4649
parserOptions: {
4750
ecmaVersion: 2017,

scripts/rollup/validate/eslintrc.fb.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ module.exports = {
3838

3939
// jest
4040
jest: true,
41+
42+
// act
43+
IS_REACT_ACT_ENVIRONMENT: true,
4144
},
4245
parserOptions: {
4346
ecmaVersion: 5,

0 commit comments

Comments
 (0)