{"action":"created","issue":{"url":"https://api.github.com/repos/sinonjs/sinon/issues/738","repository_url":"https://api.github.com/repos/sinonjs/sinon","labels_url":"https://api.github.com/repos/sinonjs/sinon/issues/738/labels{/name}","comments_url":"https://api.github.com/repos/sinonjs/sinon/issues/738/comments","events_url":"https://api.github.com/repos/sinonjs/sinon/issues/738/events","html_url":"https://github.com/sinonjs/sinon/issues/738","id":70484865,"number":738,"title":"Resolving native ES6 promises doesn't trigger callbacks when using fake timers","user":{"login":"ropez","id":431220,"avatar_url":"https://avatars.githubusercontent.com/u/431220?v=3","gravatar_id":"","url":"https://api.github.com/users/ropez","html_url":"https://github.com/ropez","followers_url":"https://api.github.com/users/ropez/followers","following_url":"https://api.github.com/users/ropez/following{/other_user}","gists_url":"https://api.github.com/users/ropez/gists{/gist_id}","starred_url":"https://api.github.com/users/ropez/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ropez/subscriptions","organizations_url":"https://api.github.com/users/ropez/orgs","repos_url":"https://api.github.com/users/ropez/repos","events_url":"https://api.github.com/users/ropez/events{/privacy}","received_events_url":"https://api.github.com/users/ropez/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":15,"created_at":"2015-04-23T18:50:31Z","updated_at":"2016-08-08T14:47:57Z","closed_at":"2016-08-08T12:44:41Z","body":"In the following test, I expected the callback for the resolved promise to be invoked while inside the test. Apparently, native promises doesn't invoke callbacks synchronously, but schedules them to be called in a manner similar to `setTimeout(callback, 0)`. However, it doesn't actually use setTimeout, so sinon's implementation of fake timers doesn't trigger the callback when calling `tick()`.\r\n\r\n```coffeescript\r\ndescribe 'Promise', ->\r\n  beforeEach ->\r\n    @clock = sinon.useFakeTimers()\r\n  afterEach ->\r\n    @clock.restore()\r\n    console.log 'teardown'\r\n\r\n  it \"should invoke callback\", ->\r\n    p = new Promise (resolve, reject) ->\r\n      console.log 'resolving'\r\n      resolve(42)\r\n      console.log 'resolved'\r\n    p.then ->\r\n      console.log 'callback'\r\n    @clock.tick()\r\n    console.log \"test finished\"\r\n```\r\n\r\nI expect this output:\r\n\r\n```\r\nresolving\r\nresolved\r\ncallback\r\ntest finished\r\nteardown\r\n```\r\n\r\nInstead I get this:\r\n\r\n```\r\nresolving\r\nresolved\r\ntest finished\r\nteardown\r\ncallback\r\n```\r\n\r\nThe callback is invoked after the test is finished, so assertions based on what happens inside the callback fail.\r\n\r\nIt makes no difference whether the promise is resolved before or after calling `then()`."},"comment":{"url":"https://api.github.com/repos/sinonjs/sinon/issues/comments/238260907","html_url":"https://github.com/sinonjs/sinon/issues/738#issuecomment-238260907","issue_url":"https://api.github.com/repos/sinonjs/sinon/issues/738","id":238260907,"user":{"login":"fatso83","id":618076,"avatar_url":"https://avatars.githubusercontent.com/u/618076?v=3","gravatar_id":"","url":"https://api.github.com/users/fatso83","html_url":"https://github.com/fatso83","followers_url":"https://api.github.com/users/fatso83/followers","following_url":"https://api.github.com/users/fatso83/following{/other_user}","gists_url":"https://api.github.com/users/fatso83/gists{/gist_id}","starred_url":"https://api.github.com/users/fatso83/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fatso83/subscriptions","organizations_url":"https://api.github.com/users/fatso83/orgs","repos_url":"https://api.github.com/users/fatso83/repos","events_url":"https://api.github.com/users/fatso83/events{/privacy}","received_events_url":"https://api.github.com/users/fatso83/received_events","type":"User","site_admin":false},"created_at":"2016-08-08T14:47:57Z","updated_at":"2016-08-08T14:47:57Z","body":"P.S. I see the tip from @ropez was to use `promise-polyfill`, and that is indeed our recommendation for a minimal polyfill too, but for the opposite reason of the one given. @mroderick patched that project in January (a few months after @ropez comment) to cache the reference to `setTimeout` so that the promise would resolve _no matter what sinon was doing_ to the global `setTimeout` reference. See taylorhakes/promise-polyfill#15 for details."}}
{"actor":{"display_login":"fatso83"}}