Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
jupiter authored May 24, 2017
2 parents 975b984 + 8e41fd5 commit f902f1f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @param {Object} obj
* @param {String} key
* @param {Function|Value} [mockValue]
* @param {Function|*} [mockValue]
* @return {Function} mock
* @api public
*/
Expand Down Expand Up @@ -164,12 +164,12 @@
}

newFn.resolveWith = function (value) {
if (simple.Promise.when) return newFn.returnWith(simple.Promise.when(value))
return newFn.returnWith(simple.Promise.resolve(value))
if (simple.Promise.when) return newFn.callFn(function createResolvedPromise () { return simple.Promise.when(value) })
return newFn.callFn(function createResolvedPromise () { return simple.Promise.resolve(value) })
}

newFn.rejectWith = function (value) {
return newFn.returnWith(simple.Promise.reject(value))
return newFn.callFn(function createRejectedPromise () { return simple.Promise.reject(value) })
}

newFn.callFn = function (fn) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-mock",
"version": "0.7.3",
"version": "0.8.0",
"description": "Super simple stubs and spies with 1-step sandbox restore",
"license": "MIT",
"main": "index.js",
Expand Down
32 changes: 32 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,10 @@ describe('simple', function () {
})
})

afterEach(function () {
simple.restore()
})

describe('with a single resolving configuration', function () {
beforeEach(function () {
stubFn = simple.stub().resolveWith('example')
Expand Down Expand Up @@ -990,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 f902f1f

Please sign in to comment.