Skip to content

Commit

Permalink
Add test that shows that rejectWith promises generates warnings
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Håkan Canberger committed Feb 17, 2017
1 parent e2e48ec commit 8bf9923
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions test.js
Expand Up @@ -994,6 +994,34 @@ describe('simple', function () {
})
})

describe('when mock rejectsWith', function () {

it('and mock is called at a later time then no UnhandledPromiseRejectionWarning and PromiseRejectionHandledWarning occurs', function (done) {
var warnings = []
function logWarning () {
var message = arguments.length === 2 ? 'Unhandled promise rejection' : 'Promise rejection was handled asynchronously'
warnings.push(message)
}
process.on('rejectionHandled', logWarning)
process.on('unhandledRejection', logWarning)

var mock = simple.mock().rejectWith(new Error('from rejectWith'))
setTimeout(function () {
mock().then(function () {
return Promise.reject('Mock should have been rejected')
}, function () {
assert.equal(warnings.length, 0, 'Warnings "' + warnings.join('", "') + '"')
})
.then(done, done)
.then(function () {
process.removeListener('rejectionHandled', logWarning)
process.removeListener('unhandledRejection', logWarning)
})
})
})

})

describe('#noLoop', function () {

it('should disable looping', function () {
Expand Down

0 comments on commit 8bf9923

Please sign in to comment.