Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e1f7952
expose unstable_interact for batching actions in tests
Feb 1, 2019
5407e88
move to TestUtils
Feb 1, 2019
e8a03d0
move it all into testutils
Feb 1, 2019
723f347
s/interact/act
Feb 1, 2019
ac7416d
warn when calling hook-like setState outside batching mode
Feb 2, 2019
1993be9
pass tests
Feb 2, 2019
3c18517
merge-temp
Feb 2, 2019
b98d051
Merge branch 'flush-sync-effects' into warn-jsdom-setstate
Feb 2, 2019
324fe0e
move jsdom test to callsite
Feb 2, 2019
e7ebd84
mark failing tests
Feb 2, 2019
0d3b45a
pass most tests (except one)
Feb 3, 2019
20a959d
augh IE
Feb 3, 2019
ee4ebb9
pass fuzz tests
Feb 3, 2019
4cb92ad
better warning, expose the right batchedUpdates on TestRenderer for www
Feb 3, 2019
aa1d531
move it into hooks, test for dom
Feb 3, 2019
00d0614
expose a flag on the host config, move stuff around
Feb 3, 2019
1b2e657
rename, pass flow
Feb 3, 2019
86a443e
pass flow... again
Feb 3, 2019
cec1ed1
tweak .act() type
Feb 3, 2019
a216c15
Merge remote-tracking branch 'upstream/master' into flush-sync-effects
Feb 3, 2019
6fa4202
enable for all jest environments/renderers; pass (most) tests.
Feb 4, 2019
6cba8f5
pass all tests
Feb 4, 2019
f946089
expose just the warning from the scheduler
Feb 4, 2019
8617b64
don't return values
Feb 5, 2019
3bfa963
Merge remote-tracking branch 'upstream/master' into flush-sync-effects
Feb 5, 2019
8f30593
Merge branch 'master' into flush-sync-effects
Feb 5, 2019
c126f10
a bunch of changes.
Feb 5, 2019
790ce2a
fixes and nits
Feb 5, 2019
32c7e1a
"fire events that udpates state"
Feb 5, 2019
5873317
nit
Feb 5, 2019
0a4b3cf
🙄
Feb 5, 2019
b6689e1
my bad
Feb 5, 2019
26a6db5
hi andrew
Feb 5, 2019
25148d7
fix .act return value testing when result === null
Feb 5, 2019
e840861
Merge remote-tracking branch 'upstream/master' into flush-sync-effects
Feb 5, 2019
0692100
nit
Feb 5, 2019
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
Prev Previous commit
Next Next commit
fixes and nits
  • Loading branch information
Sunil Pai committed Feb 5, 2019
commit 790ce2ac6642df36f727e4fc86b1b9368e2abaf0
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ describe('ReactHooksInspectionIntegration', () => {
React.useMemo(() => state1 + state2, [state1]);

function update() {
setState('A');
dispatch({value: 'B'});
act(() => {
setState('A');
});
act(() => {
dispatch({value: 'B'});
});
ref.current = 'C';
}
let memoizedUpdate = React.useCallback(update, []);
Expand Down Expand Up @@ -122,7 +126,7 @@ describe('ReactHooksInspectionIntegration', () => {
{name: 'Callback', value: updateStates, subHooks: []},
]);

act(updateStates);
updateStates();

childFiber = renderer.root.findByType(Foo)._currentFiber();
tree = ReactDebugTools.inspectHooksOfFiber(childFiber);
Expand All @@ -134,7 +138,7 @@ describe('ReactHooksInspectionIntegration', () => {
{name: 'LayoutEffect', value: effect, subHooks: []},
{name: 'Effect', value: effect, subHooks: []},
{name: 'ImperativeHandle', value: outsideRef.current, subHooks: []},
{name: 'Memo', value: 'AB', subHooks: []},
{name: 'Memo', value: 'Ab', subHooks: []},
{name: 'Callback', value: updateStates, subHooks: []},
]);
});
Expand Down
10 changes: 7 additions & 3 deletions packages/react-dom/src/__tests__/ReactTestUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ describe('ReactTestUtils', () => {
expect(button.innerHTML).toBe('2');
expect(() => setValueRef(1)).toWarnDev(
[
'It looks like you are in a test environment, trying to set state outside of an act(...) call.',
'An update to App inside a test was not wrapped in ReactTestUtils.act(...).',
],
{withoutStack: 1},
);
Expand Down Expand Up @@ -668,14 +668,18 @@ describe('ReactTestUtils', () => {

it('warns if you return a value inside act', () => {
expect(() => act(() => 123)).toWarnDev(
['An .act(...) function must not return anything'],
[
'The callback passed to ReactTestUtils.act(...) function must not return anything.',
],
{withoutStack: true},
);
});

it('warns if you try to await an .act call', () => {
expect(act(() => {}).then).toWarnDev(
['Do not await an act(...) call, it is not a promise'],
[
'Do not await the result of calling ReactTestUtils.act(...), it is not a Promise.',
],
{withoutStack: true},
);
});
Expand Down
11 changes: 7 additions & 4 deletions packages/react-dom/src/test-utils/ReactTestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,20 +391,23 @@ const ReactTestUtils = {
SimulateNative: {},

act(callback: () => void): Thenable {
// note: keep these warning messages in sync with
// createReactNoop.js and ReactTestRenderer.js
const result = ReactDOM.unstable_batchedUpdates(callback);
if (__DEV__) {
if (result !== undefined) {
let addendum;
if (typeof result.then === 'function') {
addendum =
'\n\nIt looks like you wrote act(async () => ...) or returned a Promise. ' +
'Do not write async logic inside act(...)\n';
'\n\nIt looks like you wrote ReactTestUtils.act(async () => ...), ' +
'or returned a Promise from the callback passed to it. ' +
'Putting asynchronous logic inside ReactTestUtils.act(...) is not supported.\n';
} else {
addendum = ' You returned: ' + result;
}
warningWithoutStack(
false,
'An .act(...) function must not return anything.%s',
'The callback passed to ReactTestUtils.act(...) function must not return anything.%s',
addendum,
);
}
Expand All @@ -417,7 +420,7 @@ const ReactTestUtils = {
if (__DEV__) {
warningWithoutStack(
false,
'Do not await an act(...) call, it is not a promise',
'Do not await the result of calling ReactTestUtils.act(...), it is not a Promise.',
);
}
},
Expand Down
10 changes: 6 additions & 4 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,20 +872,22 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {

// maybe this should exist only in the test file
act(callback: () => void): Thenable {
// note: keep these warning messages in sync with
// ReactTestRenderer.js and ReactTestUtils.js
let result = NoopRenderer.batchedUpdates(callback);
if (__DEV__) {
if (result !== undefined) {
let addendum;
if (typeof result.then === 'function') {
addendum =
'\n\nIt looks like you wrote act(async () => ...) or returned a Promise. ' +
'Do not write async logic inside act(...)\n';
"\n\nIt looks like you wrote ReactNoop.act(async () => ...) or returned a Promise from it's callback. " +
'Putting asynchronous logic inside ReactNoop.act(...) is not supported.\n';
} else {
addendum = ' You returned: ' + result;
}
warningWithoutStack(
false,
'An .act(...) function must not return anything.%s',
'The callback passed to ReactNoop.act(...) function must not return anything.%s',
addendum,
);
}
Expand All @@ -898,7 +900,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
if (__DEV__) {
warningWithoutStack(
false,
'Do not await an act(...) call, it is not a promise',
'Do not await the result of calling ReactNoop.act(...), it is not a Promise.',
);
}
},
Expand Down
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ function dispatchAction<S, A>(
}
if (__DEV__) {
if (shouldWarnForUnbatchedSetState === true) {
warnIfNotCurrentlyBatchingInDev();
warnIfNotCurrentlyBatchingInDev(fiber);
}
}
scheduleWork(fiber, expirationTime);
Expand Down
17 changes: 11 additions & 6 deletions packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1790,15 +1790,20 @@ function scheduleWorkToRoot(fiber: Fiber, expirationTime): FiberRoot | null {
return root;
}

export function warnIfNotCurrentlyBatchingInDev(): void {
export function warnIfNotCurrentlyBatchingInDev(fiber: Fiber): void {
if (__DEV__) {
if (isBatchingUpdates === false) {
if (isRendering === false && isBatchingUpdates === false) {
warningWithoutStack(
false,
'It looks like you are in a test environment, trying to ' +
'set state outside of an act(...) call. ' +
'This could lead to unexpected ui while testing. Use ' +
'act(...) to batch your updates and remove this warning.',
'An update to %s inside a test was not wrapped in ReactTestUtils.act(...).\n\n' +
'When testing, code that causes React state updates should be wrapped into ReactTestUtils.act(...):\n\n' +
'ReactTestUtils.act(() => {\n' +
' /* fire events */\n' +
'});\n' +
'/* assert on the output */\n\n' +
"This ensures that you're testing the behavior the user would see in the browser." +
' Learn more at https://fb.me/react-testutils-act',
getComponentName(fiber.type),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,18 +764,14 @@ describe('ReactHooksWithNoopRenderer', () => {
ReactNoop.render(<Counter count={0} />);
expect(ReactNoop.flush()).toEqual(['Count: (empty)']);
expect(ReactNoop.getChildren()).toEqual([span('Count: (empty)')]);
act(() => {
ReactNoop.flushPassiveEffects();
});
ReactNoop.flushPassiveEffects();
expect(ReactNoop.clearYields()).toEqual(['Schedule update [0]']);
expect(ReactNoop.flush()).toEqual(['Count: 0']);

ReactNoop.render(<Counter count={1} />);
expect(ReactNoop.flush()).toEqual(['Count: 0']);
expect(ReactNoop.getChildren()).toEqual([span('Count: 0')]);
act(() => {
ReactNoop.flushPassiveEffects();
});
ReactNoop.flushPassiveEffects();
expect(ReactNoop.clearYields()).toEqual(['Schedule update [1]']);
expect(ReactNoop.flush()).toEqual(['Count: 1']);
});
Expand All @@ -797,10 +793,7 @@ describe('ReactHooksWithNoopRenderer', () => {
expect(ReactNoop.getChildren()).toEqual([span('Count: (empty)')]);

// Rendering again should flush the previous commit's effects
expect(() => ReactNoop.render(<Counter count={1} />)).toWarnDev(
['It looks like you are in a test environment'],
{withoutStack: true},
);
ReactNoop.render(<Counter count={1} />);
ReactNoop.flushThrough(['Schedule update [0]', 'Count: 0']);
expect(ReactNoop.getChildren()).toEqual([span('Count: (empty)')]);

Expand All @@ -810,7 +803,7 @@ describe('ReactHooksWithNoopRenderer', () => {

expect(ReactNoop.getChildren()).toEqual([span('Count: 0')]);

act(() => ReactNoop.flushPassiveEffects());
ReactNoop.flushPassiveEffects();
expect(ReactNoop.flush()).toEqual(['Schedule update [1]', 'Count: 1']);
expect(ReactNoop.getChildren()).toEqual([span('Count: 1')]);
});
Expand Down Expand Up @@ -913,9 +906,7 @@ describe('ReactHooksWithNoopRenderer', () => {
expect(ReactNoop.flush()).toEqual(['Count: (empty)']);
expect(ReactNoop.getChildren()).toEqual([span('Count: (empty)')]);
// Now fire the effects
act(() => {
ReactNoop.flushPassiveEffects();
});
ReactNoop.flushPassiveEffects();
// There were multiple updates, but there should only be a
// single render
expect(ReactNoop.clearYields()).toEqual(['Count: 0']);
Expand Down
10 changes: 5 additions & 5 deletions packages/react-test-renderer/src/ReactTestRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,21 +565,21 @@ const ReactTestRendererFiber = {
unstable_setNowImplementation: setNowImplementation,

act(callback: () => void): Thenable {
// note: keep these warning messages in sync with
let result = batchedUpdates(callback);
// warnings copied from ReactTestUtils
if (__DEV__) {
if (result !== undefined) {
let addendum;
if (typeof result.then === 'function') {
addendum =
'\n\nIt looks like you wrote act(async () => ...) or returned a Promise. ' +
'Do not write async logic inside act(...)\n';
"\n\nIt looks like you wrote TestRenderer.act(async () => ...) or returned a Promise from it's callback. " +
'Putting asynchronous logic inside TestRenderer.act(...) is not supported.\n';
} else {
addendum = ' You returned: ' + result;
}
warningWithoutStack(
false,
'An .act(...) function must not return anything.%s',
'The callback passed to TestRenderer.act(...) function must not return anything.%s',
addendum,
);
}
Expand All @@ -592,7 +592,7 @@ const ReactTestRendererFiber = {
if (__DEV__) {
warningWithoutStack(
false,
'Do not await an act(...) call, it is not a promise',
'Do not await the result of calling TestRenderer.act(...), it is not a Promise.',
);
}
},
Expand Down