Sinon: Stubs should have a `resolves` and `rejects` for setting promise results

Created on 8 Aug 2015  ·  13Comments  ·  Source: sinonjs/sinon

This is very simple syntax sugar:

var stub = sinon.stub();
stub.resolves("hello");
//is the same as
stub.returns(Promise.resolve("hello"));

stub.rejects(new Error("oh noes!"));
//is the same as
stub.returns(Promise.reject(new Error("oh noes!"));
Medium Feature Request Help wanted

Most helpful comment

Can I do a sinon.stub().returnsArg(1) where the stub returns a promise? I am trying to just return the argument as the resolve of my promise. Does not seem to work.

All 13 comments

That would be some nice sugar, thank you for the suggestion @mariusGundersen

Our team has a little library on npm which has been helping us with this kind of thing - https://github.com/substantial/sinon-stub-promise - although the way it's presently implemented it requires stub syntax like: sinon.stub().returnsPromise(). Thoughts? It looks like from the example above that this assumes synchronous evaluation, right?

I really like @mariusGundersen's suggestion. As far as I know, all promise libraries are async all the time, right? If so I think the stub promise should mirror this behavior.

I just figured it would use the globally available Promise.resolve, if one exists, and if not it wouldn't work. But letting it return an object with a then method is a much better solution:

var stub = sinon.stub(); 
stub.resolves("hello"); 
//is the same as 
stub.returns({then: x => setTimeout(()  => x("hello"), 0)}); 
stub.rejects(new Error("oh noes!")); 
//is the same as 
stub.returns({then: x => undefined, catch: x => setTimeout(() => x(new Error("oh noes!")), 0)});

(written on phone, might have errors)

Our team wrote a little lib to provide similar functionality. Maybe this will help. It uses the bluebird library for promises. https://www.npmjs.com/package/sinon-bluebird

+1 for native Promise support

There's also a project called sinon-as-promised which patches your project's existing sinon module using native-promise-only to add resolves/rejects semantics to stubs.

It'd be ace if this or the changes from one of the other modules could be added to sinon itself.

Can I do a sinon.stub().returnsArg(1) where the stub returns a promise? I am trying to just return the argument as the resolve of my promise. Does not seem to work.

@jnystrom usage questions belong on the mailing list, but this is not something sinon currently supports as syntactic sugar. Perhaps you could suggest a syntax? Something like this should do what you want, and you don't need sinon for it.

stub = (arg) => Promise.resolve(arg);

Same from here: #738

Hello everyone, first of all, thanks for the excellent work you guys have been doing on Sinon.
If you'd like to have this implemented I'd also love to give it a shot, just let me know and I'll start working at a PR for that.

@lucasfcosta, thanks for that. I think @mroderick talked long ago about just integrating sinon-as-promised into Sinon, wasn't that so? That would be the easiest solution, and I have no objections.

@lucasfcosta I would love to see a PR to integrate sinon-as-promised into Sinon's master branch

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tinganho picture tinganho  ·  3Comments

NathanHazout picture NathanHazout  ·  3Comments

ndhoule picture ndhoule  ·  4Comments

byohay picture byohay  ·  3Comments

akdor1154 picture akdor1154  ·  4Comments