Skip to content
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

Create rejectWith Promises when mock is called #24

Merged
merged 4 commits into from
May 24, 2017

Conversation

HCanber
Copy link
Contributor

@HCanber HCanber commented Feb 17, 2017

Fixes #22

Since node 6.9, if a promise is rejected and no error handler is attached to the promise within a turn of the event loop, or if an error handler was attached to it later than one turn of the Node.js event loop, then warnings are logged (and in future versions of node it will terminate the node process).

Since rejectWith creates the promise when it is called, and not when the mock is called, this means that the following code will generate warnings:

const mock = simple.mock().rejectWith(new Error())
setTimeout(() => mock().then( () =>..., err => ...), 0)

This PR demonstrates this by first adding a test, and then fixes it by create the rejected promise when the mock is called.

Håkan Canberger added 4 commits February 17, 2017 19:01
This test demonstrates that promises are created at configuration time
and not when the mock is called.

From docs:
  The 'rejectionHandled' event is emitted whenever a Promise has been
  rejected and an error handler was attached to it ... later than one
  turn of the Node.js event loop.
  ...
  The 'unhandledRejection' event is emitted whenever a Promise is
  rejected and no error handler is attached to the promise within a
  turn of the event loop.
  https://nodejs.org/api/process.html#process_event_rejectionhandled
Copy link
Owner

@jupiter jupiter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent

@jupiter jupiter merged commit 1d41cad into jupiter:master May 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When a stub is not called, rejectWith results in UnhandledPromiseRejectionWarning
2 participants