Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
sinon-as-promised
Advanced tools
Extend Sinon stubs with promise stubbing methods.
Sinon 2 added resolves
and rejects
methods and no longer requires this library.
npm install sinon-as-promised
If you're using sinon-as-promised in the browser and are not using Browserify/Webpack, use 3.x or earlier.
var sinon = require('sinon')
require('sinon-as-promised')
sinon.stub().resolves('foo')().then(function (value) {
assert.equal(value, 'foo')
})
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. It defaults to using native ES6 Promise (or provides a polyfill), but you can use another promise library if you'd like, as long as it exposes a constructor:
// Using Bluebird
var Bluebird = require('bluebird')
require('sinon-as-promised')(Bluebird)
stub.resolves(value)
-> stub
Required
Type: any
When called, the stub will return a "thenable" object which will return a promise for the provided value
. Any Promises/A+ compliant library will handle this object properly.
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(err)
-> stub
Required
Type: error
/ string
When called, the stub will return a thenable which will return a reject promise with the provided err
. If err
is a string, it will be set as the message on an Error
object.
stub.rejects(new Error('foo'))().catch(function (error) {
// error.message === 'foo'
});
stub.rejects('foo')().catch(function (error) {
// error.message === 'foo'
});
stub.onCall(0).rejects('bar');
stub().catch(function (error) {
// error.message === 'bar'
});
MIT © Ben Drucker
FAQs
Sugar methods for using sinon.js stubs with promises
The npm package sinon-as-promised receives a total of 13,785 weekly downloads. As such, sinon-as-promised popularity was classified as popular.
We found that sinon-as-promised demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.