Announcing: Chai as Promised

Showing 1-12 of 12 messages
Announcing: Chai as Promised Domenic Denicola 3/29/12 12:15 PM
Re: Announcing: Chai as Promised Stepan Riha 3/29/12 2:11 PM
This looks awesome, but can we also get the assert equivalents for this? We prefer using the assert.* syntax because it better matches our non-JS unit tests.

I can envision the following:

  1. assert.rejects(promise, message);
  2. assert.rejects(promise, /pattern/, message); 
  3. assert.rejects(promise, TypeError, message);

  4. assert.resolves(promise, message);
  5. assert.resolves(promise, function validator(), massage);

That the message is what's to be displayed if the assertion fails. You may also want to have breaks and fulfills as aliases for rejects and resolves.
The validator() function should be passed the arguments that the promise resolved with and it should return true if the resolution values are ok.

The resolves/rejects assert should be returning another promise (which resolves/rejects based on whether the assertions succeeded) s.t. I can easily chain the assertions.

- Stepan
Re: Announcing: Chai as Promised Jake L. 3/30/12 12:14 AM
Amazing work Domenic! Looking forward to trying this out.
RE: Announcing: Chai as Promised Domenic Denicola 4/1/12 4:02 PM

For sure; I can work on assert over the upcoming week.


Recall that, while broken and rejected are synonyms, there’s a difference between “resolved” and “fulfilled.” There are two dichotomies: pending vs. resolved, and fulfilled versus rejected. Thus, a rejected promise is still resolved.


My full vision:


assert.isRejected(promise, message);

assert.isRejected(promise, /pattern/, message);

assert.isRejected(promise, TypeError, message);

// + assert.isBroken synonyms


assert.isFulfilled(promise, message);


assert.eventually.isNumber(promise, message);

assert.eventually.typeOf(promise, "string", message);

// etc.


And as you say, they will return promises so that you can chain.


From: [] On Behalf Of Stepan Riha
Sent: Thursday, March 29, 2012 17:11
Subject: Re: Announcing: Chai as Promised

RE: Announcing: Chai as Promised Domenic Denicola 4/1/12 4:40 PM
I am considering the following breaking change. Please let me know what you think. TL;DR version:

promiseFor(2 + 2), done)

would no longer work, to be replaced by

promiseFor(2 + 2)

== The problem ==

When building 1.0, I was concerned with supporting the `.then(done, done)` style for frameworks like Mocha, wherein you are given a single `done` callback to signal the end of an async test. If you pass it nothing, it assumes the test passed; otherwise, the test fails. The idea is that `done` is somewhat like a Node.js callback.

Thus, when running the following code:

promiseFor(2 + 2), done);

while `be` returns an assertion-promise [1] for 42, `ok` actually returns a promise for `undefined`, since otherwise `done` would be called with 42 and assume an error occurred.

This forces an unfortunate situation. The most visible consequence is that you cannot chain assertions. That is,

promiseFor(2 + 2), done);

does not work, since `ok` ends the chaining and must return a promise fulfilled with `undefined`, so by the time `equal` gets called, it doesn’t have anything to test `4` against.

The other problem is that the way in which this is coded [2] is rather fragile, as I need to manually categorize asserters into "end of chain" or "still chaining." Currently this is done by saying that all function asserters (e.g. `equal`, `include`, `keys`) are "end of chain," as well as a small whitelist of property-asserters (viz. `arguments`, `empty`, `exist`, `false`, `ok`, and `true`). All others are assumed to be "still chaining." This is not only future-unfriendly, it is also plugin-unfriendly.

[1]: that is, a promise that has all of the asserters, like `ok`, `equal`, etc., tacked onto it

== The proposed solution ==

Chai as Promised would no longer take pains to fulfill assertion-promises with `undefined`. That is,

promiseFor(2 + 2), done);

would now throw up, since `4` would be passed to `done`. The naïve solution is then

promiseFor(2 + 2) () { done(); }, done);

But of course I would then just augment assertion-promises with a new capstone method to do this for you, e.g.

promiseFor(2 + 2) // kind of conflicts with Q
promiseFor(2 + 2) // probably my favorite?
promiseFor(2 + 2)
promiseFor(2 + 2) // mostly kidding
promiseFor(2 + 2) // definitely kidding [3]

What do you think?

[3]: 4/1/12 11:23 PM <This message has been deleted.>
Re: Announcing: Chai as Promised Jake L. 4/1/12 11:25 PM
I definitely see the issue here. 

My preference is `end`, however I do not use Q so I am not sure of what kind of conflict this would cause. If that is an issue than make both `callback` with an alias to `cb` available for use. 
Re: Announcing: Chai as Promised Stepan Riha 4/2/12 5:27 AM
Good point.  I was looking at it from the emitters point of view and both jQuery's and CommonJS/A implementation (when.js, node-promise) use the resolve() method to fulfill a promise.

So, yeah, isFulfilled() is unambiguously successful.

FWIW, I would not add an "isResolved" assertion to indicate a promise is no longer in the unresolved state (I, and other looking at my test code, would probably expect assert.isResolved() to fail on a rejected promise).  I might be better to have another, unrelated, term dealing with the transition from the unresolved state (e.g. isAnswered or some such).
Re: Announcing: Chai as Promised Stepan Riha 4/2/12 5:36 AM
We don't use fluid assertions (didn't come to unit tests from Ruby, so it still feels forced to me and I don't like the fact that should fails on undefined values).

Having said that, could you use notify?  As in:

promiseFor(2 + 2) 

- Stepan

BTW, my personal preference would most definitely be whyDoesntHeJustAcceptMochaPullRequestNumber329(done). :-)

Re: Announcing: Chai as Promised Jake L. 4/5/12 1:17 PM
@Stepan - Just a reminder there is also should.exist(somevar) to ensure prevent non-assertion errors when you run into issue where something might not be defined. I just realized those are not yet properly documented. Added issue
Re: Announcing: Chai as Promised Domenic Denicola 4/9/12 1:02 PM
OK, 2.0 is released! It has an `assert` interface, as well as the breaking change discussed earlier, with Stephan's `notify` being the winner.

Re: Announcing: Chai as Promised Jake L. 4/9/12 1:42 PM
Very cool!