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

Testing RSVP.Promises #10

Closed
mixonic opened this issue Nov 26, 2012 · 3 comments
Closed

Testing RSVP.Promises #10

mixonic opened this issue Nov 26, 2012 · 3 comments

Comments

@mixonic
Copy link

mixonic commented Nov 26, 2012

Howdy! In writing some tests for code using https://github.com/tildeio/rsvp.js, we've run into unexpected behavior. The fulfilled assertion helper is always passing, regardless of if the promise is actually rejected.

Promises in RSVP are deferred in different ways depending on your env. In Node, they are deferred with nextTick. In Firefox and Webkit with MutationObserver and a disconnected DOM node. In all other envs with setTimeout. See https://github.com/tildeio/rsvp.js/blob/master/lib/rsvp.js#L3 for detail.

This deferral means sinon's normal fake timers don't force the code to be synchronous. I was hoping this is the problem chai-as-promised was addressing. Here is a fiddle of chai-as-promised allowing an assertion that should be false:

http://jsfiddle.net/TqA5R/9/

@domenic
Copy link
Collaborator

domenic commented Nov 26, 2012

No, Chai as Promised does not try to make promises fake-synchronous. If you want to expand the featureset of Sinon.JS to support different methods of fake-sync, feel free to file a bug there. It is not in scope for Chai as Promised.

If you don't try to hide the asynchronicity in your tests, you can either test using the notify helper:

chai.expect(this.promise).to.be.fulfilled.notify(done);

or using Mocha as Promised.

@domenic domenic closed this as completed Nov 26, 2012
@mixonic
Copy link
Author

mixonic commented Nov 26, 2012

Righto! This is behaving as expected http://jsfiddle.net/TqA5R/11/

    it("EXPECTED TO FAIL! should not be fulfilled on reject", function(done) {
        this.promise.reject();
        chai.expect(this.promise).to.be.fulfilled.notify(done);
    });
    it("should be fulfilled on resolve", function(done) {
        this.promise.resolve();
        chai.expect(this.promise).to.be.fulfilled.notify(done);
    });

The expected to fail fails. Thanks for the tip @domenic !

@domenic
Copy link
Collaborator

domenic commented Nov 26, 2012

Glad to help! BTW I'm pretty sure the "done invoked with non-Error" is JSFiddle specific, since they use iframes and so instanceof Error fails cross-frame.

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

No branches or pull requests

2 participants