Fork notes
Original repo used es2015 code. This one is:
- transpiled using Babel to work on older JavaScript environments.
- upgraded eslint to 4, and moved it to dev deps.
Chai Assertions for Promises
Chai as Promised extends Chai with a fluent language for asserting facts about promises.
Instead of manually wiring up your expectations to a promise's fulfilled and rejected handlers:
;
you can write code that expresses what you really mean:
return shouldeventually;
or if you have a case where return
is not preferable (e.g. style considerations) or not possible (e.g. the testing framework doesn't allow returning promises to signal asynchronous test completion), then you can use the following workaround (where done()
is supplied by the test framework):
shouldeventually;
Notice: either return
or notify(done)
must be used with promise assertions. This can be a slight departure from the existing format of assertions being used on a project or by a team. Those other assertions are likely synchronous and thus do not require special handling.
How to Use
should
/expect
Interface
The most powerful extension provided by Chai as Promised is the eventually
property. With it, you can transform any existing Chai assertion into one that acts on a promise:
2 + 2should; // becomesreturn Promiseshouldeventually; tohave; // becomesreturn toeventuallyhave;
There are also a few promise-specific extensions (with the usual expect
equivalents also available):
return promiseshouldbefulfilled;return promiseshouldeventuallydeep;return promiseshould; // same as `.eventually.deep.equal`return promiseshouldberejected;return promiseshouldbe; // other variants of Chai's `throw` assertion work too.
assert
Interface
As with the should
/expect
interface, Chai as Promised provides an eventually
extender to chai.assert
, allowing any existing Chai assertion to be used on a promise:
assert; // becomesreturn asserteventually;
And there are, of course, promise-specific extensions:
return assert; return assert;return assert; return assert;return assert;return assert;
Progress Callbacks
Chai as Promised does not have any intrinsic support for testing promise progress callbacks. The properties you would want to test are probably much better suited to a library like Sinon.JS, perhaps in conjunction with Sinon–Chai:
var progressSpy = sinon; return promise;
Customizing Output Promises
By default, the promises returned by Chai as Promised's assertions are regular Chai assertion objects, extended with a single then
method derived from the input promise. To change this behavior, for instance to output a promise with more useful sugar methods such as are found in most promise libraries, you can override chaiAsPromised.transferPromiseness
. Here's an example that transfer's Q's finally
and done
methods:
chaiAsPromised { assertionthen = promisethen; // this is all you get by default assertionfinally = promisefinally; assertiondone = promisedone;};
Transforming Arguments to the Asserters
Another advanced customization hook Chai as Promised allows is if you want to transform the arguments to the asserters, possibly asynchronously. Here is a toy example:
chaiAsPromised { return args;} Promiseshouldeventually; // will now fail!Promiseshouldeventually; // will now pass!
The transform can even be asynchronous, returning a promise for an array instead of an array directly. An example of that might be using Promise.all
so that an array of promises becomes a promise for an array. If you do that, then you can compare promises against other promises using the asserters:
// This will normally fail, since within() only works on numbers.Promiseshouldeventuallybe; chaiAsPromised { return Promiseallargs;}; // But now it will pass, since we transformed the array of promises for numbers into// (a promise for) an array of numbersPromiseshouldeventuallybe;
Compatibility
Chai as Promised is compatible with all promises following the Promises/A+ specification.
Notably, jQuery's promises were not up to spec before jQuery 3.0, and Chai as Promised will not work with them. In particular, Chai as Promised makes extensive use of the standard transformation behavior of then
, which jQuery<3.0 does not support.
Angular promises have a special digest cycle for their processing, and need extra setup code to work with Chai as Promised.
Working with Non-Promise–Friendly Test Runners
Some test runners (e.g. Jasmine, QUnit, or tap/tape) do not have the ability to use the returned promise to signal asynchronous test completion. If possible, I'd recommend switching to ones that do, such as Mocha, Buster, or blue-tape. But if that's not an option, Chai as Promised still has you covered. As long as your test framework takes a callback indicating when the asynchronous test run is over, Chai as Promised can adapt to that situation with its notify
method, like so:
; ;
In these examples, if the conditions are not met, the test runner will receive an error of the form "expected promise to be fulfilled but it was rejected with [Error: error message]"
, or "expected promise to be rejected but it was fulfilled."
There's another form of notify
which is useful in certain situations, like doing assertions after a promise is complete. For example:
;
Notice how .notify(done)
is hanging directly off of .should
, instead of appearing after a promise assertion. This indicates to Chai as Promised that it should pass fulfillment or rejection directly through to the testing framework. Thus, the above code will fail with a Chai as Promised error ("expected promise to be fulfilled…"
) if promise
is rejected, but will fail with a simple Chai error (expected "before" to equal "after"
) if otherState
does not change.
Multiple Promise Assertions
To perform assertions on multiple promises, use Promise.all
to combine multiple Chai as Promised assertions:
;
This will pass any failures of the individual promise assertions up to the test framework, instead of wrapping them in an "expected promise to be fulfilled…"
message as would happen if you did return Promise.all([…]).should.be.fulfilled
. If you can't use return
, then use .should.notify(done)
, similar to the previous examples.
Installation and Setup
Node
Do an npm install chai-as-promised
to get up and running. Then:
var chai = ;var chaiAsPromised = ; chai; // Then either:var expect = chaiexpect;// or:var assert = chaiassert;// or:chai;// according to your preference of assertion style
You can of course put this code in a common test fixture file; for an example using Mocha, see the Chai as Promised tests themselves.
Note when using other Chai plugins: Chai as Promised finds all currently-registered asserters and promisifies them, at the time it is installed. Thus, you should install Chai as Promised last, after any other Chai plugins, if you expect their asserters to be promisified.
In the Browser
To use Chai as Promised in environments that don't support Node.js-like CommonJS modules, you'll need to use a bundling tool like browserify. See also the note below about browser compatibility.
Karma
If you're using Karma, check out the accompanying karma-chai-as-promised plugin.
Browser/Node Compatibility
See fork notes at the top.
Chai as Promised requires Node v4+ or a browser with equivalent support for modern JavaScript syntax. If your browser doesn't support modern JavaScript syntax, you'll need to transpile it down using a tool like Babel.