-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
Changing state too quickly cause error with shallow render #7927
Comments
Sawtaytoes
changed the title
Too many calls to state change too quickly cause error with shallow render
Changing state too quickly cause error with shallow render
Oct 10, 2016
Here's an updated fail case with less code and which works in node: 'use strict'
const React = require('react')
const enzyme = require('enzyme')
const jsdom = require('jsdom')
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>')
global.document = doc
global.window = doc.defaultView
class Root extends React.Component {
componentWillMount() {
// Will work if this is not deferred
setTimeout(this.testBreak.bind(this), 1)
}
testBreak() {
// Will work if we do not setState here
this.setState({})
}
render() {
// Will work if we remove at least one `a`. This bug manifests itself with multiple children.
// It doesn't matter if the children are in an Array.
return React.createElement('div', null, React.createElement('a'), React.createElement('a'))
}
}
enzyme.mount(React.createElement(Root))
const TestReactElement = () => React.createElement('div')
// Will work if we use `mount` instead of shallow rendering
const testRender = () => enzyme.shallow(
React.createElement(TestReactElement)
)
// Will work if we call testRender < 3x
testRender()
testRender()
testRender() Edit: updated with code comments. |
Here's another example that uses a pubsub event-emitter which simulates what a test would look like if it was hooked up to something like flux: 'use strict'
const React = require('react')
const enzyme = require('enzyme')
const jsdom = require('jsdom')
const transmitter = require('transmitter')
const bus = transmitter()
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>')
global.document = doc
global.window = doc.defaultView
class Root extends React.Component {
componentDidMount() {
// Flux-like
bus.subscribe(() => this.setState({}))
}
render() {
// Will work if we remove at least one `a`. This bug manifests itself with multiple children.
// It doesn't matter if the children are in an Array.
return React.createElement('div', null, React.createElement('a'), React.createElement('i'))
}
}
// Works if we use shallow rendering here
enzyme.mount(React.createElement(Root))
const TestReactElement = () => React.createElement('section')
// Will work if we use `mount` instead of shallow rendering
const testRender = () => enzyme.shallow(
React.createElement(TestReactElement)
)
// Will work if we call testRender < 3x
testRender()
testRender()
testRender()
// Once we call an action in a separate test, it'll fail.
bus.publish(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?
Running
ReactTestUtils.createRenderer().render()
causes react to throw an exception:I have the same issue when using both
Component
andPureComponent
.I do not see the error when using
ReactTestUtils.renderToDocument()
.If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/reactjs/69z2wepo/).
Could not figure out how to use
ReactTestUtils
in that JSFiddle example even though react-with-addons is loaded.Here's the code I used in my project to reproduce it:
It works fine so long as I run
testRender()
once. As soon as it's run 2 or more times, the error pops up.In my project, TAP output is being listened to from a
console.log
wrapper andstate
is being updated when a new one comes in. So while my reproducible example is contrived, it's real-world bug when running tests in my project.What is the expected behavior?
The expected behavior is that no error occurs in the console when running multiple shallow test renders while at the same time React is updating the state of an element rendered to the DOM.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
15.3.0
and15.3.2
. Windows 10 64-bit v1607 (and updates up to October 10th, 2016). I've not tried this in previous versions of React. Verified it displays the error in Chrome54.0.2840.50 beta-m (64-bit)
and Firefox50.0b5
.The text was updated successfully, but these errors were encountered: