Sinon: Enhancement: Add ability to override global promise library

Created on 22 Mar 2017  路  16Comments  路  Source: sinonjs/sinon

  • Sinon version : 2.1.0
  • Other libraries you are using: Bluebird

Hi,

I want to be able to override the default promise library with an external library such as Bluebird or Q.

We don't want to override the Global Promise library (which is the native Promise library) but instead import/require it when needed.

Would you be interested in me providing a Pull Request for this or would I have to create a fork.

Thanks in advance,

Most helpful comment

@lumaxis [email protected] is now on npm

All 16 comments

Can you please elaborate why you think your use case needs a change in Sinon?

How do you imagine using it?

Like sinon-as-promised does it?

Use case is that our code uses Bluebird additions like .tap() and .finally(). Stubbing with the native Promises does not really work.

We used to use sinon-as-promised and that allowed using Bluebird. Now I have horrible kludge in test suites that add such missing functions to the native Promise prototype and I would really like to get rid of that.

Yes, that is our issue as well.

At the moment, our code looks like this

sinon.stub(someObject, 'someMethod').callsFake(() => return bb.Promise.resolve({}));

I would want to work like this though.

const bb = require('bluebird');
const sinon = required('sinon');

sinon.promiseLibrary = bb.Promise
sinon.stub(someObject, 'someMethod').resolves({});
sinon.stub(someObject, 'someOtherMethod').resolves({});

or

const bb = require('bluebird');
const sinon = required('sinon');

sinon.stub(someObject, 'someMethod').resolves({}); // Would default to global promise
sinon.stub(someObject, 'someMethod').resolves({}, bb.Promise); // World use included promise

I think it would be important to be able to select the specific library you want to resolve with as sometimes we work with various libraries that return various promise types.

We could add a configure call like sinon-test? https://github.com/sinonjs/sinon-test#usage

Or

sinon({ promiseLibrary: bb.Promise }).stub();

Yeah, as long as we configure an instance of Sinon, and not set defaults globally I don't mind either way. We fought long and hard to get rid of globals before the release of version 2, so let's not introduce more 馃槃

Please see https://github.com/sinonjs/sinon/pull/1364

Documentation hasn't been written yet as I wanted to run past with you guys the interface first.

The reason I used .setPromise was so that it didn't muck with any of the other interfaces and so it is absolutely clear as to what it is doing.

I thought that I could probably implement setPromise on a sandbox or sinon object as well.

@fatso83 @Kynde @mantoni

Hi @fatso83 @Kynde @mantoni @mroderick,

This should be ready to go unless someone has any other comments?

Thanks in advance,

@blacksun1 Your contribution is probably fine, but I am travelling around Japan untill May (with just a cheap cell phone), so someone else needs to dig into this. We are poorly paid for these efforts, so we need to take turns when we have some spare time :-)

Never mind my previous comment - I was able to get at this anyway :-)

Maybe someone else in @sinonjs/sinon-core would like to publish a new minor version?

Any chance of getting a release with this soon? 馃檪

@lumaxis [email protected] is now on npm

@mroderick Thanks a lot! 馃檹 馃帀

Was this page helpful?
0 / 5 - 0 ratings