-
Notifications
You must be signed in to change notification settings - Fork 49.2k
using the wrong renderer's act() should warn #15756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
5b186a6
e789900
a2dae63
6d64194
669d92e
c0dc398
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,44 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>sanity test for ReactTestUtils.act</title> | ||
</head> | ||
<body> | ||
this page tests whether act runs properly in a browser. | ||
<br/> | ||
your console should say "5" | ||
<script src='scheduler-unstable_mock.development.js'></script> | ||
<script src='react.development.js'></script> | ||
<script type="text/javascript"> | ||
window.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler = window.SchedulerMock | ||
</script> | ||
<script src='react-dom.development.js'></script> | ||
<script src='react-dom-test-utils.development.js'></script> | ||
<script> | ||
async function run(){ | ||
<head> | ||
<title>sanity test for ReactTestUtils.act</title> | ||
</head> | ||
<body> | ||
this page tests whether act runs properly in a browser. | ||
<br /> | ||
your console should say "5" | ||
<script src="scheduler-unstable_mock.development.js"></script> | ||
<script src="react.development.js"></script> | ||
<script type="text/javascript"> | ||
window.React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler = | ||
window.SchedulerMock; | ||
</script> | ||
<script src="react-dom.development.js"></script> | ||
<script src="react-dom-test-utils.development.js"></script> | ||
<script> | ||
// from ReactTestUtilsAct-test.js | ||
function App() { | ||
let [state, setState] = React.useState(0); | ||
async function ticker() { | ||
await null; | ||
setState(x => x + 1); | ||
} | ||
React.useEffect( | ||
() => { | ||
ticker(); | ||
}, | ||
[Math.min(state, 4)], | ||
); | ||
React.useEffect(() => { | ||
ticker(); | ||
}, [Math.min(state, 4)]); | ||
return state; | ||
} | ||
const el = document.createElement('div'); | ||
await ReactTestUtils.act(async () => { | ||
ReactDOM.render(React.createElement(App), el); | ||
}); | ||
// all 5 ticks present and accounted for | ||
console.log(el.innerHTML); | ||
} | ||
run(); | ||
|
||
</script> | ||
</body> | ||
|
||
async function testAsyncAct() { | ||
const el = document.createElement("div"); | ||
await ReactTestUtils.act(async () => { | ||
ReactDOM.render(React.createElement(App), el); | ||
}); | ||
// all 5 ticks present and accounted for | ||
console.log(el.innerHTML); | ||
} | ||
|
||
testAsyncAct(); | ||
</script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import TestUtils from 'react-dom/test-utils'; | ||
import TestRenderer from 'react-test-renderer'; | ||
|
||
let spy; | ||
beforeEach(() => { | ||
spy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
|
||
function confirmWarning() { | ||
expect(spy).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
"It looks like you're using the wrong act() around your test interactions." | ||
), | ||
'' | ||
); | ||
} | ||
|
||
function App(props) { | ||
return 'hello world'; | ||
} | ||
|
||
it("doesn't warn when you use the right act + renderer: dom", () => { | ||
TestUtils.act(() => { | ||
TestUtils.renderIntoDocument(<App />); | ||
}); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("doesn't warn when you use the right act + renderer: test", () => { | ||
TestRenderer.act(() => { | ||
TestRenderer.create(<App />); | ||
}); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('works with createRoot().render combo', () => { | ||
const root = ReactDOM.unstable_createRoot(document.createElement('div')); | ||
TestRenderer.act(() => { | ||
root.render(<App />); | ||
}); | ||
confirmWarning(); | ||
}); | ||
|
||
it('warns when using the wrong act version - test + dom: render', () => { | ||
TestRenderer.act(() => { | ||
TestUtils.renderIntoDocument(<App />); | ||
}); | ||
confirmWarning(); | ||
}); | ||
|
||
it('warns when using the wrong act version - test + dom: updates', () => { | ||
let setCtr; | ||
function Counter(props) { | ||
const [ctr, _setCtr] = React.useState(0); | ||
setCtr = _setCtr; | ||
return ctr; | ||
} | ||
TestUtils.renderIntoDocument(<Counter />); | ||
TestRenderer.act(() => { | ||
setCtr(1); | ||
}); | ||
confirmWarning(); | ||
}); | ||
|
||
it('warns when using the wrong act version - dom + test: .create()', () => { | ||
TestUtils.act(() => { | ||
TestRenderer.create(<App />); | ||
}); | ||
confirmWarning(); | ||
}); | ||
|
||
it('warns when using the wrong act version - dom + test: .update()', () => { | ||
let root; | ||
// use the right one here so we don't get the first warning | ||
TestRenderer.act(() => { | ||
root = TestRenderer.create(<App key="one" />); | ||
}); | ||
TestUtils.act(() => { | ||
root.update(<App key="two" />); | ||
}); | ||
confirmWarning(); | ||
}); | ||
|
||
it('warns when using the wrong act version - dom + test: updates', () => { | ||
let setCtr; | ||
function Counter(props) { | ||
const [ctr, _setCtr] = React.useState(0); | ||
setCtr = _setCtr; | ||
return ctr; | ||
} | ||
const root = TestRenderer.create(<Counter />); | ||
TestUtils.act(() => { | ||
setCtr(1); | ||
}); | ||
confirmWarning(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,7 +173,7 @@ const ceil = Math.ceil; | |
const { | ||
ReactCurrentDispatcher, | ||
ReactCurrentOwner, | ||
ReactShouldWarnActingUpdates, | ||
ReactActingRendererSigil, | ||
} = ReactSharedInternals; | ||
|
||
type WorkPhase = 0 | 1 | 2 | 3 | 4 | 5 | 6; | ||
|
@@ -2276,11 +2276,40 @@ function warnAboutInvalidUpdatesOnClassComponentsInDEV(fiber) { | |
} | ||
} | ||
|
||
export function warnIfNotScopedWithMatchingAct(fiber: Fiber): void { | ||
if (__DEV__) { | ||
if ( | ||
ReactActingRendererSigil.current !== null && | ||
// use the function flushPassiveEffects directly as the sigil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Outdated comment |
||
// so this comparison is expected here | ||
ReactActingRendererSigil.current !== flushPassiveEffects | ||
) { | ||
// it looks like we're using the wrong matching act(), so log a warning | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment kinda repeats the warning? |
||
warningWithoutStack( | ||
false, | ||
"It looks like you're using the wrong act() around your test interactions.\n" + | ||
'Be sure to use the matching version of act() corresponding to your renderer:\n\n' + | ||
'// for react-dom:\n' + | ||
"import {act} from 'react-test-utils';\n" + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there's no such package either 🤦♂️ |
||
'//...\n' + | ||
'act(() => ...);\n\n' + | ||
'// for react-test-renderer:\n' + | ||
"import TestRenderer from 'react-test-renderer';\n" + | ||
'const {act} = TestRenderer;\n' + | ||
'//...\n' + | ||
'act(() => ...);' + | ||
'%s', | ||
getStackByFiberInDevAndProd(fiber), | ||
); | ||
} | ||
} | ||
} | ||
|
||
function warnIfNotCurrentlyActingUpdatesInDEV(fiber: Fiber): void { | ||
if (__DEV__) { | ||
if ( | ||
workPhase === NotWorking && | ||
ReactShouldWarnActingUpdates.current === false | ||
ReactActingRendererSigil.current !== flushPassiveEffects | ||
) { | ||
warningWithoutStack( | ||
false, | ||
|
Uh oh!
There was an error while loading. Please reload this page.