Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stubs should have a resolves and rejects for setting promise results #804

Closed
mariusGundersen opened this issue Aug 8, 2015 · 13 comments
Closed

Comments

@mariusGundersen
Copy link

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!"));
@mroderick
Copy link
Member

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

@mkornblum
Copy link

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?

@cjohansen
Copy link
Contributor

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.

@mariusGundersen
Copy link
Author

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)

@toddbluhm
Copy link

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

@screendriver
Copy link

+1 for native Promise support

@achingbrain
Copy link

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.

@jnystrom
Copy link

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.

@fatso83
Copy link
Contributor

fatso83 commented Nov 18, 2015

@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);

@otroboe
Copy link

otroboe commented Aug 1, 2016

Same from here: #738

@lucasfcosta
Copy link
Member

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.

@fatso83
Copy link
Contributor

fatso83 commented Dec 14, 2016

@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.

@mroderick
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests