-
Notifications
You must be signed in to change notification settings - Fork 48.8k
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
Merged
threepointone
merged 6 commits into
facebook:master
from
threepointone:wrong-renderer-act-warning
May 29, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5b186a6
warn when using the wrong renderer's act around another renderer's up…
threepointone e789900
unneeded (and wrong) comment
threepointone a2dae63
run the dom fixture on CI
threepointone 6d64194
update the sigil only in __DEV__
threepointone 669d92e
remove the obnoxious comment
threepointone c0dc398
use an explicit export for the sigil
threepointone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.