sinon-as-promised

Sugar methods for using sinon.js stubs with promises

MIT 18 个版本
安装
npm install sinon-as-promised
yarn add sinon-as-promised
pnpm add sinon-as-promised
bun add sinon-as-promised
README

sinon-as-promised Build Status

Extend Sinon stubs with promise stubbing methods.

Sinon 2 added resolves and rejects methods and no longer requires this library.

Installing

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.

Usage

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)

API

stub.resolves(value) -> stub

value

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

err

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'
});

Examples

License

MIT © Ben Drucker

版本列表
4.0.3 2017-03-21
4.0.2 2016-07-06
4.0.0 2015-06-03
3.0.1 2016-07-06
3.0.0 2015-03-31
2.0.3 2014-12-11
2.0.2 2014-12-11
2.0.1 2014-12-11
2.0.0 2014-12-11
1.4.1 2014-11-18
1.4.0 2014-11-18
1.3.0 2014-10-08
1.2.0 2014-09-26
1.1.0 2014-09-04
1.0.0 2014-08-20
0.0.4 2014-05-22
0.0.3 2014-03-07
0.0.2 2014-03-07