{"action":"created","issue":{"url":"https://api.github.com/repos/facebook/react/issues/11775","repository_url":"https://api.github.com/repos/facebook/react","labels_url":"https://api.github.com/repos/facebook/react/issues/11775/labels{/name}","comments_url":"https://api.github.com/repos/facebook/react/issues/11775/comments","events_url":"https://api.github.com/repos/facebook/react/issues/11775/events","html_url":"https://github.com/facebook/react/issues/11775","id":279361125,"number":11775,"title":"Testing asynchronous setState of React","user":{"login":"aaronplanell","id":13084403,"avatar_url":"https://avatars3.githubusercontent.com/u/13084403?v=4","gravatar_id":"","url":"https://api.github.com/users/aaronplanell","html_url":"https://github.com/aaronplanell","followers_url":"https://api.github.com/users/aaronplanell/followers","following_url":"https://api.github.com/users/aaronplanell/following{/other_user}","gists_url":"https://api.github.com/users/aaronplanell/gists{/gist_id}","starred_url":"https://api.github.com/users/aaronplanell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaronplanell/subscriptions","organizations_url":"https://api.github.com/users/aaronplanell/orgs","repos_url":"https://api.github.com/users/aaronplanell/repos","events_url":"https://api.github.com/users/aaronplanell/events{/privacy}","received_events_url":"https://api.github.com/users/aaronplanell/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-12-05T12:42:52Z","updated_at":"2017-12-05T12:56:46Z","closed_at":null,"author_association":"NONE","body":"Hello!!!\r\n\r\nI'd read this article of Dave Ceddia: [Snapshot Testing React Components with Jest](https://semaphoreci.com/community/tutorials/snapshot-testing-react-components-with-jest). It's very useful but, I've got a big doubt about how Jest works (and I'm not the only one) and I don't understand how Jest can manage the asynchronous process when `setState` is called. \r\n\r\nFor instance, in the example of the article, `increment` is a function that calls `setState` and increments its value. Perfect. But, what about this test?\r\n```\r\nit('should render 2', () => {\r\n  const component = renderer.create(<PositiveCounter/>);\r\n  component.getInstance().increment();\r\n  component.getInstance().increment();\r\n  expect(component.toJSON()).toMatchSnapshot();\r\n});\r\n```\r\nHow can we assure that the `expect` will be called after the `setState` calls will be ended? And what about the second call of `increment`?\r\n\r\nI strongly believe that I've got a misconception about this process, but I'm not the only one. Any help will be very well received.\r\n\r\nThanks a lot in advance!\r\n\r\nPS: Redirected from https://github.com/facebook/jest/issues/5015 "},"comment":{"url":"https://api.github.com/repos/facebook/react/issues/comments/349296385","html_url":"https://github.com/facebook/react/issues/11775#issuecomment-349296385","issue_url":"https://api.github.com/repos/facebook/react/issues/11775","id":349296385,"user":{"login":"jquense","id":339286,"avatar_url":"https://avatars2.githubusercontent.com/u/339286?v=4","gravatar_id":"","url":"https://api.github.com/users/jquense","html_url":"https://github.com/jquense","followers_url":"https://api.github.com/users/jquense/followers","following_url":"https://api.github.com/users/jquense/following{/other_user}","gists_url":"https://api.github.com/users/jquense/gists{/gist_id}","starred_url":"https://api.github.com/users/jquense/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jquense/subscriptions","organizations_url":"https://api.github.com/users/jquense/orgs","repos_url":"https://api.github.com/users/jquense/repos","events_url":"https://api.github.com/users/jquense/events{/privacy}","received_events_url":"https://api.github.com/users/jquense/received_events","type":"User","site_admin":false},"created_at":"2017-12-05T12:56:46Z","updated_at":"2017-12-05T12:56:46Z","author_association":"COLLABORATOR","body":"`setState` is not asynchronous in the way we usually mean in JavaScript. That is is to say that calls don't actually happen on another tick of the event loop, so from the outside of a react tree everything looks synchronous. What I mean by that is that setState calls may be batched together, so each call enqueues some update, but the update isn't processed until its that components \"turn\". Now that may actually be changing (changed?) with fibers async rendering, which is _actually_ async in the way we usually mean. For that I think we'll need some top level hooks to catch when a component is done rendering for realz."}}
{"actor":{"display_login":"jquense"}}