From 8bf9923379095ec09fd1d5db161428f748658f61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Akan=20Canberger?= Date: Fri, 17 Feb 2017 19:14:19 +0100 Subject: [PATCH] Add test that shows that rejectWith promises generates warnings 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 --- test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test.js b/test.js index 77eac58..bb4aa8c 100644 --- a/test.js +++ b/test.js @@ -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 () {