Last active
August 29, 2015 14:16
-
-
Save sebv/6fd1d5bcbaf7ae70df48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var chai = require('chai'), | |
chaiAsPromised = require('chai-as-promised'), | |
Q = require('q'); | |
chai.use(chaiAsPromised); | |
expect = chai.expect; | |
describe('mocha + chai-as-promised examples', function () { | |
var badPromise = function() { | |
return Q.delay(500).then(function() { | |
throw new Error('Crash'); | |
}); | |
}; | |
var goodPromise = function(n) { | |
return Q.delay(500).then(function() { | |
return n; | |
}); | |
}; | |
describe('example 1: catching promise rejection', function() { | |
it('this is wrong, wrongly passes', function() { | |
var p = badPromise(); | |
expect(p).to.eventually.equal(5); | |
}); | |
it('this is correct (returning promise), fails', function() { | |
var p = badPromise(); | |
// you need to return the promise to mocha | |
return expect(p).to.eventually.equal(5); | |
}); | |
it('this is correct (using callbacks), fails', function(done) { | |
var p = badPromise(); | |
// passing callback to mocha instead of returning | |
expect(p).to.eventually.equal(5).notify(done); | |
}); | |
}); | |
describe('example 2: wrong promise result', function() { | |
it('this is wrong, wrongly passes', function() { | |
var p = goodPromise(5); | |
expect(p).to.eventually.equal(3); | |
}); | |
it('this is correct (returning promise), fails', function() { | |
var p = goodPromise(5); | |
// you need to return the promise to mocha | |
return expect(p).to.eventually.equal('3'); | |
}); | |
it('this is correct (using callbacks), fails', function(done) { | |
var p = goodPromise(5); | |
// passing callback to mocha instead of returning | |
expect(p).to.eventually.equal('3').notify(done); | |
}); | |
}); | |
describe('example 3: good promise result', function() { | |
it('looks like it passes, but does not check anything', function() { | |
var p = goodPromise(5); | |
expect(p).to.eventually.equal(5); | |
}); | |
it('to illustrate previous example, this passes', function() { | |
var p = goodPromise(5); | |
expect(p).to.eventually.equal(4); | |
}); | |
it('correct way, using return', function() { | |
var p = goodPromise(5); | |
return expect(p).to.eventually.equal(5); | |
}); | |
it('correct way, using callback', function(done) { | |
var p = goodPromise(5); | |
return expect(p).to.eventually.equal(5).notify(done); | |
}); | |
}); | |
describe('example 4: waiting for several promises', function() { | |
it('this is wrong, passes', function() { | |
var p1 = goodPromise(5); | |
var p2 = goodPromise(5); | |
var p3 = goodPromise(5); | |
expect(p1).to.eventually.equal(4); | |
expect(p2).to.eventually.equal(4); | |
expect(p3).to.eventually.equal(4); | |
}); | |
it('also wrong, not checking anything', function() { | |
var p1 = goodPromise(5); | |
var p2 = goodPromise(5); | |
var p3 = goodPromise(5); | |
expect(p1).to.eventually.equal(5); | |
expect(p2).to.eventually.equal(5); | |
expect(p3).to.eventually.equal(5); | |
}); | |
it('correct, sequentially, but bad pattern', function() { | |
var p1 = goodPromise(5); | |
var p2 = goodPromise(5); | |
var p3 = goodPromise(5); | |
return expect(p1).to.eventually.equal(5) | |
.then(function() { | |
return expect(p2).to.eventually.equal(5); | |
}).then(function() { | |
return expect(p3).to.eventually.equal(5); | |
}); | |
}); | |
it('also correct, sequentially, but bad pattern, fails', function() { | |
var p1 = goodPromise(5); | |
var p2 = goodPromise(5); | |
var p3 = goodPromise(5); | |
return expect(p1).to.eventually.equal(5) | |
.then(function() { | |
return expect(p2).to.eventually.equal(5); | |
}).then(function() { | |
return expect(p3).to.eventually.equal(3); | |
}); | |
}); | |
it('not correct, internal return missing, wrongly passes', function() { | |
var p1 = goodPromise(5); | |
var p2 = goodPromise(5); | |
var p3 = goodPromise(5); | |
// this return is correct | |
return expect(p1).to.eventually.equal(5) | |
.then(function() { | |
return expect(p2).to.eventually.equal(5); | |
}).then(function() { | |
//missing return | |
expect(p3).to.eventually.equal(3); | |
}); | |
}); | |
it('better way, parallel , fails', function() { | |
var p1 = goodPromise(5); | |
var p2 = goodPromise(5); | |
var p3 = goodPromise(5); | |
return Q.all([ | |
expect(p1).to.eventually.equal(5), | |
expect(p2).to.eventually.equal(5), | |
expect(p3).to.eventually.equal(3) | |
]); | |
}); | |
it('even better, parallel not using chai-as-promised, fails', function() { | |
var p1 = goodPromise(5); | |
var p2 = goodPromise(5); | |
var p3 = goodPromise(5); | |
return Q.all([p1, p2, p3]).spread(function(res1, res2, res3) { | |
expect(res1).to.equal(5); | |
expect(res2).to.equal(5); | |
expect(res3).to.equal(3); | |
}); | |
}); | |
it('using become, fails', function() { | |
var p1 = goodPromise(5); | |
var p2 = goodPromise(5); | |
var p3 = goodPromise(5); | |
return expect(Q.all([p1, p2, p3])).to.become([5, 5, 3]); | |
}); | |
}); | |
describe('example 5: using become instead of eventually.equal', function() { | |
// become is actually equivalent to eventually.deep.equal | |
it('using eventually.equal, fails', function() { | |
var p1 = goodPromise(5); | |
return expect(p1).to.eventually.equal(4); | |
}); | |
it('using become, fails', function() { | |
var p1 = goodPromise(5); | |
return expect(p1).to.become(4); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment