Giter Club home page Giter Club logo

sinon-bluebird's Introduction

Travis CI Coveralls NPM version Downloads NPM license JavaScript Style Guide

sinon-bluebird

Obsolete: This package is obsolete as sinon now natively supports the .resolves and .rejects methods along with setting a global promise constructor via the .usingPromise() method.

A plugin that adds bluebird promise helper methods to Sinon.

Installation

Run npm install sinon-bluebird

or git clone then npm install

Optionally run unit tests: npm test

Dependencies

This package requires that both sinon and bluebird already be installed.

This package has peerDependencies of the following:

  • sinon 1.x
  • bluebird 3.x

If you need a lower version, create an issue and it can be adjusted as necessary. Please, if you do need a lower version, test it first and then make the request.

Usage

// Require in the libs
var sinon = require('sinon')
var BPromise = require('bluebird')
require('sinon-bluebird')

////// -- Stubs Usage -- //////
// Create an example function
var obj = {
  foo: function foo() {
    return 'bar'
  }
}

// Stub a function that returns a resolved bluebird BPromise
sinon.stub(obj, 'foo').resolves('hello world!')

// Execute the stub function
obj.foo().then(function(val) {
  // val === 'hello world!'
})

// Restore the original method
obj.foo.restore()

// Stub a method that returns a rejected bluebird BPromise
// Note: For shorthand, just pass in a string and it will be
// internally wrapped in an Error object (removed in >= v2.0.0)
sinon.stub(obj, 'foo').rejects('AHHHHHH!!!!')

// Execute the stub function
obj.foo().catch(function(e) {
  // e === new Error('AHHHHHH!!!!') [<2.0.0]
  // e === 'AHHHHHH!!!!' [>=2.0.0]
});

// Restore back to the original function
obj.foo.restore()

// Original method back to normal
obj.foo() // === 'bar'
////// -- End Stubs Usage -- //////

////// -- Spies Usage -- //////

var obj = {
  returnMethod: function (val) {
    return BPromise.resolve(val)
  },
  paramMethod: function (val, prom, val2) {
    return true
  }
}

// Return methods
var spy = sinon.spy(obj, 'returnMethod')

obj.returnMethod('Hello') //execute the test function
spy.returnedPromise('Hello') // === true

obj.returnMethod('World') //execute the test function a second time
spy.alwaysReturnedPromise('Hello') // === false

spy.restore()

//Called With methods
spy = sinon.spy(obj, 'paramMethod')

obj.paramMethod(BPromise.resolve('Hello')) //pass in a promise
spy.calledWithPromise('Hello') // === true
obj.paramMethod.reset() //reset spy

/* Pass in a promise mixed with regular values
 * Note the rejected promise passed in, any rejected promise will possibly show up in console.log
 * due to how bluebird reports possibly unhandled exceptions (even though in this case we are
 * intentionally passing in a rejected promise)
*/
obj.paramMethod('Hello', BPromise.reject('World'), '!')
spy.calledWithMatch('Hello', 'World', String)  // === true (match allows for comparison by type too!)

obj.paramMethod.restore() //restore the original method back

////// -- End Spies Usage -- //////

API

Stubs

.resolves(value)

Returns a resolved bluebird promise with the given value

.rejects(value)

Returns a rejected bluebird promise with the given value.

Note: If the given value is a String, that string will be wrapped in an Error object and will be on the message property. (Removed in >=2.0.0)

Spies

All spy methods are identical to sinons version except it automatically unwraps the bluebird promise (if a promise is returned) and compares directly to the unwrapped value. We append the word Promise to each method to denote it unwraps a promise.

Return Value Methods

Promises cannot be in a pending state otherwise an error will be thrown.

.returnedPromise(value)

.alwaysReturnedPromise(value)

Called With Methods

The promise (or promises) can be anywhere in the list of arguments passed into the spied on function or none at all.

Promises cannot be in a pending state otherwise an error will be thrown.

.calledWithPromise(value, ...)

.calledWithMatchPromise(value, ...)

.alwaysCalledWithPromise(value, ...)

.alwaysCalledWithMatchPromise(value, ...)

.calledWithExactlyPromise(value, ...)

.alwaysCalledWithExactlyPromise(value, ...)

Inspiration

Thanks to sinon-as-promised for inspiration.

If you're wondering what the main differences are:

  • sinon-as-promised allows other promise libraries to be used instead of bluebird (sinon-bluebird is designed and optimized for use only with bluebird)
  • sinon-as-promised only supports .then, .catch, and .finally methods off of the stub (no special bluebird methods like: .map, .bind, .spread, etc...)

Contributors

License

MIT

sinon-bluebird's People

Contributors

bmoeskau avatar srussellextensis avatar toddbluhm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sinon-bluebird's Issues

.stub().rejects() and .stub().returns(Promise.reject(...)) returning different values

.rejects() and .returns(Promise.reject()) are returning two different values. I would expect them to return the same, since .resolves() and .returns(Promise.resolve()) do so.

var sinon = require('sinon-bluebird'),
  BPromise = require('bluebird'),
  x;

x = sinon.stub().rejects('goodbye');
x().catch(function(err){
  console.log(err, typeof err); // [Error: goodbye] 'object'
});

x = sinon.stub().returns(BPromise.reject('goodbye'));
x().catch(function(err){
  console.log(err, typeof err); // goodbye string
});

Next major version of Bluebird

Recently a new major version of bluebird (3.0.5) was released.
But your library has strict requirement of peerDependency

Can you update your package.json and also release next major version?
Seems like there are no breaking changes

Declares sinon as a dependency, and sometimes monkey-patches its own copy

It seems sinon is declared as a dependency. Since we use sinon < 1.12, sinon-bluebird was getting its own copy in sinon-bluebird/node_modules, and attaching the new keywords there.

{
  "dependencies": {
    "bluebird": "^2.9.12",
    "sinon": "^1.12.2"
  },
  "peerDependencies": {
    "sinon": "1.x"
  }
}

It's also declared as a peerDependency, which sounds good, so maybe dependencies is an accident? Since it's used in the tests, should it be a devDependency instead?

Support Sinon 2

Sinon 2 seems to be unsupported. I get:

TypeError: Cannot set property 'resolves' of undefined
    at Object.<anonymous> (...\node_modules\sinon-bluebird\index.js:10:47)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.