Socket
Socket
Sign inDemoInstall

sinon-as-promised

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sinon-as-promised

Sugar methods for using sinon.js stubs with promises


Version published
Weekly downloads
17K
increased by16.62%
Maintainers
1
Weekly downloads
 
Created
Source

sinon-as-promised Build Status NPM version

Sugar methods for using sinon.js stubs with promises.

Installing

# via npm:
$ npm install sinon-as-promised
# or bower:
$ bower install sinon-as-promised

Tagged versions (which Bower uses) include a ./release/sinon-as-promised.js build that expect sinon to be available as window.sinon. The normal version in ./src used by npm expects to be able to require('sinon').

Setup

var sinon           = require('sinon');
var sinonAsPromised = require('sinon-as-promised');

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, but you can use another promise library if you'd like, as long as it exposes a constructor:

// Using RSVP
var RSVP            = require('rsvp');
var sinonAsPromised = require('sinon-as-promised')(RSVP.Promise);
// ES6 promises
var sinonAsPromised = require('sinon-as-promised')(Promise);

API

stub.resolves(value)

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(error)

When called, the stub will return a thenable which will return a reject promise with the provided error. If error 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'
});

Examples

License

MIT

Keywords

FAQs

Package last updated on 11 Dec 2014

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc