# sinon-as-promised

> Sugar methods for using sinon.js stubs with promises

Latest version **4.0.3** (published 2017-03-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install sinon-as-promised
pnpm add sinon-as-promised
yarn add sinon-as-promised
bun add sinon-as-promised
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.3 |
| Published | 2017-03-21 |
| First published | 2014-03-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/sinon-as-promised) |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 137 |
| Author | Ben Drucker |
| Maintainers | bendrucker |
| Keywords | sinon, promises, test |

## Links

- npm: https://www.npmjs.com/package/sinon-as-promised
- Repository: https://github.com/bendrucker/sinon-as-promised
- Issues: https://github.com/bendrucker/sinon-as-promised/issues
- npm.io page: https://npm.io/package/sinon-as-promised

## Dependencies (2)

- [create-thenable](https://npm.io/package/create-thenable.md) ~1.0.0
- [native-promise-only](https://npm.io/package/native-promise-only.md) ~0.8.1

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 4.0.3 (latest) — 2017-03-21
- 3.0.1 — 2016-07-06
- 4.0.2 — 2016-07-06
- 4.0.0 — 2015-06-03
- 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
- … 3 more at https://npm.io/package/sinon-as-promised/versions

## README

sinon-as-promised [![Build Status](https://travis-ci.org/bendrucker/sinon-as-promised.svg?branch=master)](https://travis-ci.org/bendrucker/sinon-as-promised)
=================

> Extend [Sinon](https://github.com/cjohansen/sinon.js) stubs with promise stubbing methods.

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

## Installing
```sh
npm install sinon-as-promised
```

If you're using sinon-as-promised in the browser and are not using Browserify/Webpack, use [3.x](https://github.com/bendrucker/sinon-as-promised/tree/v3.0.1) or earlier.

## Usage

```js
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)](https://github.com/getify/native-promise-only), but you can use another promise library if you'd like, as long as it exposes a constructor:

```js
// 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+](https://promisesaplus.com/) compliant library will handle this object properly.

```js
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.

```js
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

* [angular](https://github.com/bendrucker/sinon-as-promised/tree/master/examples/angular)
* [Bluebird](https://github.com/bendrucker/sinon-as-promised/tree/master/examples/bluebird)
* [Node or Browserify](https://github.com/bendrucker/sinon-as-promised/tree/master/examples/node-browserify)

## License

MIT © [Ben Drucker](http://bendrucker.me)

---
_Source: https://npm.io/package/sinon-as-promised · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
