sinon-as-promised
Advanced tools
Comparing version 1.2.0 to 1.3.0
{ | ||
"name": "sinon-as-promised", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Sugar methods for using sinon.js stubs with promises", | ||
@@ -24,5 +24,7 @@ "main": "sinon-as-promised.js", | ||
"homepage": "https://github.com/bendrucker/sinon-as-promised", | ||
"dependencies": { | ||
"bluebird": "2" | ||
}, | ||
"devDependencies": { | ||
"chai-as-promised": "4", | ||
"bluebird": "2", | ||
"chai": "1", | ||
@@ -29,0 +31,0 @@ "sinon": "1", |
@@ -1,2 +0,2 @@ | ||
sinon-as-promised [![Build Status](https://travis-ci.org/bendrucker/sinon-as-promised.png)](https://travis-ci.org/bendrucker/sinon-as-promised) [![NPM version](https://badge.fury.io/js/sinon-as-promised.png)](http://badge.fury.io/js/sinon-as-promised) | ||
sinon-as-promised [![Build Status](https://travis-ci.org/bendrucker/sinon-as-promised.svg)](https://travis-ci.org/bendrucker/sinon-as-promised) [![NPM version](https://badge.fury.io/js/sinon-as-promised.svg)](http://badge.fury.io/js/sinon-as-promised) | ||
================= | ||
@@ -9,21 +9,33 @@ | ||
var sinon = require('sinon'); | ||
var Promise = require('bluebird'); | ||
var sinonAsPromised = require('sinon-as-promised')(Promise); | ||
var sinonAsPromised = require('sinon-as-promised'); | ||
``` | ||
You can use any promise library you'd like that exposes a constructor. You'll only need to call `sinon-as-promised` once. It attaches the appropriate stubbing functions which will then be available anywhere else you require `sinon`. You'll probably want to call it in a setup file that is required before your tests. | ||
You'll only need to require `sinon-as-promised` once. It attaches the appropriate stubbing functions which will then be available anywhere else you require `sinon`. You'll probably want to call it in a setup file that is required before your tests. It defaults to [Bluebird](https://github.com/petkaantonov/bluebird), but you can use another promise library if you'd like, as long as it exposes a constructor: | ||
```js | ||
var RSVP = require('rsvp'); | ||
var sinonAsPromised = require('sinon-as-promised')(RSVP.Promise); | ||
``` | ||
## Usage | ||
#### `stub.resolves(value)` | ||
When called, the stub will return a promise which resolves with the provided `value`. | ||
When called, the stub will return a promise which resolves with the provided `value`. | ||
```js | ||
stub.resolves('foo')().then(function (value) { | ||
var stub = sinon.stub(); | ||
stub.resolves('foo'); | ||
stub().then(function (value) { | ||
// value === 'foo' | ||
}); | ||
stub.onCall(0).resolves('bar') | ||
stub().then(function (value) { | ||
// value === 'bar' | ||
}); | ||
``` | ||
#### `stub.rejects(error)` | ||
When called, the stub will return a promise which rejects with the provided `error`. If `error` is a string, it will be set as the error message. | ||
When called, the stub will return a promise which rejects with the provided `error`. If `error` is a string, it will be set as the error message. | ||
@@ -37,5 +49,10 @@ ```js | ||
}); | ||
stub.onCall(0).rejects('bar'); | ||
stub().catch(function (error) { | ||
// error.message === 'bar' | ||
}); | ||
``` | ||
## License | ||
[MIT](LICENSE) | ||
[MIT](LICENSE) |
'use strict'; | ||
var sinon = require('sinon'); | ||
var Promise = require('bluebird'); | ||
var sinon = require('sinon'); | ||
module.exports = function (Promise) { | ||
function resolves (value) { | ||
/*jshint validthis:true */ | ||
return this.returns(new Promise(function (resolve) { | ||
process.nextTick(resolve.bind(null, value)); | ||
})); | ||
} | ||
if (!Promise) { | ||
throw new Error('A Promise constructor must be provided'); | ||
sinon.stub.resolves = resolves; | ||
sinon.behavior.resolves = resolves; | ||
function rejects (err) { | ||
if (typeof err === 'string') { | ||
err = new Error(err); | ||
} | ||
/*jshint validthis:true */ | ||
return this.returns(new Promise(function (resolve, reject) { | ||
process.nextTick(reject.bind(null, err)); | ||
})); | ||
} | ||
sinon.stub.resolves = function (value) { | ||
return this.returns(new Promise(function (resolve) { | ||
process.nextTick(resolve.bind(null, value)); | ||
})); | ||
}; | ||
sinon.stub.rejects = rejects; | ||
sinon.behavior.rejects = rejects; | ||
sinon.stub.rejects = function (err) { | ||
if (typeof err === 'string') { | ||
err = new Error(err); | ||
} | ||
return this.returns(new Promise(function (resolve, reject) { | ||
process.nextTick(reject.bind(null, err)); | ||
})); | ||
}; | ||
module.exports = function (_Promise_) { | ||
if (typeof _Promise_ !== 'function') { | ||
throw new Error('A Promise constructor must be provided'); | ||
} | ||
else { | ||
Promise = _Promise_; | ||
} | ||
return sinon; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4523
4
31
57
2
+ Addedbluebird@2
+ Addedbluebird@2.11.0(transitive)