Custom matchers for testing ES6 Promises with Jasmine 2.x.
This library is compatible with the ES6Promise polyfill library if the current browser does not support ES6 Promises. (This means it will work with Phantom JS.)
You can install with either NPM or Bower like so:
npm install jasmine-es6-promise-matchers
bower install jasmine-es6-promise-matchers
Testing promises should be simple but it's normally a bit of a hassle. Your tests probably look something like this:
function(done) {
promise.then(
function(value) {
expect(value).toBe('something');
done();
},
done.fail);
Using the matchers provided by this library your tests could instead look like this:
function(done) {
expect(promise).toBeResolvedWith('something', done);
}
To use it, just install the library before your tests begin:
beforeEach(functionJasminePromiseMatchers.install);
And uninstall it once your tests are over:
afterEach(JasminePromiseMatchers.uninstall);
Now you're ready to start testing!
This library includes a couple of matchers, shown below.
Verify that a Promise is (or will be) rejected.
expect(promise).toBeRejected(done);
Verify that a Promise is rejected with a specific value.
expect(promise).toBeRejectedWith('something', done);
// Asymmetric matching is also supported for objects:
var partialMatch = jasmine.objectContaining({partial: 'match'});
expect(promise).toBeRejectedWith(partialMatch, done);
Verify that a Promise is resolved.
expect(promise).toBeResolved(done);
Verify that a Promise is resolved with a specific value.
expect(promise).toBeResolvedWith('something', done);
// Asymmetric matching is also supported for objects:
var partialMatch = jasmine.objectContaining({partial: 'match'});
expect(promise).toBeResolvedWith(partialMatch, done);
Open an issue or toss up a pull request if you'd like to see anything added to this library!