{"action":"created","issue":{"url":"https://api.github.com/repos/domenic/chai-as-promised/issues/173","repository_url":"https://api.github.com/repos/domenic/chai-as-promised","labels_url":"https://api.github.com/repos/domenic/chai-as-promised/issues/173/labels{/name}","comments_url":"https://api.github.com/repos/domenic/chai-as-promised/issues/173/comments","events_url":"https://api.github.com/repos/domenic/chai-as-promised/issues/173/events","html_url":"https://github.com/domenic/chai-as-promised/issues/173","id":186171016,"number":173,"title":"Unhandled rejection warnings when expecting a rejection","user":{"login":"laggingreflex","id":2523987,"avatar_url":"https://avatars1.githubusercontent.com/u/2523987?v=4","gravatar_id":"","url":"https://api.github.com/users/laggingreflex","html_url":"https://github.com/laggingreflex","followers_url":"https://api.github.com/users/laggingreflex/followers","following_url":"https://api.github.com/users/laggingreflex/following{/other_user}","gists_url":"https://api.github.com/users/laggingreflex/gists{/gist_id}","starred_url":"https://api.github.com/users/laggingreflex/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/laggingreflex/subscriptions","organizations_url":"https://api.github.com/users/laggingreflex/orgs","repos_url":"https://api.github.com/users/laggingreflex/repos","events_url":"https://api.github.com/users/laggingreflex/events{/privacy}","received_events_url":"https://api.github.com/users/laggingreflex/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":16,"created_at":"2016-10-30T22:51:39Z","updated_at":"2017-11-28T15:45:54Z","closed_at":"2016-10-30T23:30:21Z","author_association":"NONE","body":"I'm not using Bluebird but similarly to [this thread](https://github.com/domenic/chai-as-promised/issues/97) I'm getting the same warning errors of unhanded rejection when actually expecting a rejection. \n\nHere's a repo to reproduce: https://github.com/laggingreflex/chai-as-promised-reject-test\n\nIt was mentioned chai-as-promised is indeed handling the errors ([here](https://github.com/domenic/chai-as-promised/blob/master/lib/chai-as-promised.js#L116)) but I think they're not being handled soon enough. Putting a console.log there shows that it reaches that code only after the warning has been displayed:\n\n```\n(node:5580) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: rejected\n  promise\n(node:5580) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)\n>>> assertIfNegated about to be called... <<<\n    √ should be rejected\n\n\n  1 passing (48ms)\n```\n"},"comment":{"url":"https://api.github.com/repos/domenic/chai-as-promised/issues/comments/347565323","html_url":"https://github.com/domenic/chai-as-promised/issues/173#issuecomment-347565323","issue_url":"https://api.github.com/repos/domenic/chai-as-promised/issues/173","id":347565323,"user":{"login":"ScottFreeCode","id":16506071,"avatar_url":"https://avatars3.githubusercontent.com/u/16506071?v=4","gravatar_id":"","url":"https://api.github.com/users/ScottFreeCode","html_url":"https://github.com/ScottFreeCode","followers_url":"https://api.github.com/users/ScottFreeCode/followers","following_url":"https://api.github.com/users/ScottFreeCode/following{/other_user}","gists_url":"https://api.github.com/users/ScottFreeCode/gists{/gist_id}","starred_url":"https://api.github.com/users/ScottFreeCode/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ScottFreeCode/subscriptions","organizations_url":"https://api.github.com/users/ScottFreeCode/orgs","repos_url":"https://api.github.com/users/ScottFreeCode/repos","events_url":"https://api.github.com/users/ScottFreeCode/events{/privacy}","received_events_url":"https://api.github.com/users/ScottFreeCode/received_events","type":"User","site_admin":false},"created_at":"2017-11-28T15:45:54Z","updated_at":"2017-11-28T15:45:54Z","author_association":"NONE","body":"Pseudo-cross-posting idea from https://github.com/mochajs/mocha/issues/3009#issuecomment-347190839\r\n\r\nI have something of a workaround for https://github.com/domenic/chai-as-promised/issues/173#issuecomment-330409841\r\n\r\n```js\r\nconst chai = require( 'chai' );\r\nconst promised = require( 'chai-as-promised' );\r\n\r\nchai.use( promised );\r\nchai.should();\r\n\r\n\r\nconst makeSomePromise = () => new Promise( () => {\r\n  throw new Error( 'rejected' );\r\n} );\r\n\r\ndescribe( 'promise', () => {\r\n  it( 'should be rejected', () => {\r\n    return makeSomePromise().should.eventually.be.rejected; // note the addition of `.eventually`\r\n  } );\r\n} );\r\n```\r\n\r\nOne thing to be aware of, although I *think* this is inherent to promises' eagerness and error handling design and will be relevant no matter whether Mocha is changed to have no tick delays\\*, is that if you recreate the promise on a per-test basis then every test must handle its rejection (even if it isn't relevant, e.g. if you're testing that it starts off pending), while on the other hand if you were to reuse a rejected promise in multiple tests then the first test must handle the rejection and once it does so there will be no warning if any subsequent test fails to do so even if it needs to (in other words, even if Mocha allowed you to assign a promise once and use it in multiple tests, it'd be almost equivalent to assigning the promise and immediately calling `somePromise.catch(()=>{})` to suppress rejection warnings for that promise). In that sense, creating a new promise for every test (whether with `beforeEach` or with a thunk as in my workaround) would be the more safe/robust approach (if, in certain unusual cases, slightly annoying).\r\n\r\n\\*(NOTE: In this example *any asynchronous test or hook* can introduce a delay between the variable assignment *outside of any test or hook* and *actually getting to run this test*; you would have to move that variable assignment into a `before`/`beforeEach` hook that runs just prior to this test in addition to Mocha removing the delays.)"}}
{"actor":{"display_login":"ScottFreeCode"}}