jupiter / simple-mock

Super simple stubs and spies with 1-step sandbox restore
MIT License
87 stars 12 forks source link

When a stub is not called, rejectWith results in UnhandledPromiseRejectionWarning #22

Closed minrwhite closed 7 years ago

minrwhite commented 7 years ago

If a stub is defined to return a rejected Promise using rejectWith, but that stub has not been called during process execution, then an UnhandledPromiseRejectionWarning will result in later versions of NodeJS (6.6.0+), with a warning that at some later version, NodeJS will terminate with a non-zero exit code on such an occurrence.

This appears to be because the Promise (both for rejectWith and resolveWith cases) is created at the point that the behaviour is defined rather than at the point the stub is called, and if the stub is not called, then there is no way for the Promise to be handled.

I'd expect that either:

  1. simple-mock should be able to report at the end of the process whenever behaviour has been defined that has not been invoked (useful for tracing unnecessary specification in tests)
  2. or at least that the creation of the Promise should be deferred until the behaviour is required, in both resolve and reject cases.