public
Last active

Asynchronous Unit Tests With Mocha And WinJS Promises

  • Download Gist
1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
describe("a test", function(){
var foo = false;
 
beforeEach(function(){
 
// simulate async call w/ setTimeout
setTimeout(function(){
foo = true;
}, 50);
 
});
 
it("should pass", function(){
expect(foo).equals(true);
});
 
});
2.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
describe("a test", function(){
var foo = false;
 
beforeEach(function(done){
 
setTimeout(function(){
foo = true;
 
// complete the async beforeEach
done();
 
}, 50);
 
});
 
it("should pass", function(){
expect(foo).equals(true);
});
 
});
3.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
describe("a test", function(){
var foo = false;
 
beforeEach(function(done){
 
var promise = new WinJS.Promise(function(complete){
complete(true);
});
 
promise.then(function(value){
 
// get the value from the completed promise
foo = value;
 
// complete the async beforeEach
done();
});
 
});
 
it("should pass", function(){
expect(foo).equals(true);
});
 
});
4.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
describe("a test", function(){
 
// ...
 
it("should pass", function(done){
 
// simulate async expecation
setTimeout(function(){
 
// complete the async expectation
done();
 
}, 50);
 
});
 
});
5.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
describe("a test", function(){
var foo = false;
var promise;
 
beforeEach(function(){
promise = new WinJS.Promise(function(complete){
 
// complete the promise with a value of false
// so that the expectation fails
complete(false);
 
});
});
 
it("should pass", function(done){
 
promise.then(function(value){
 
// expect the value to be true
// which should fail with a message
// saying it expected false to be true
expect(value).equals(true);
 
done();
});
 
});
 
});
6.js
JavaScript
1 2 3
promise
.then(thenCallback)
.done(thenCallback, errorCallback, completeCallback);
7.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
describe("a test", function(){
var foo = false;
var promise;
 
beforeEach(function(){
promise = new WinJS.Promise(function(complete){
 
// complete the promise with a value of false
// so that the expectation fails
complete(false);
 
});
});
 
it("should pass", function(done){
 
promise.then(function(value){
expect(value).equals(true);
done();
 
}).done(null, function(error){
 
// the expectation threw an error
// so forward that error to Mocha
done(error);
 
});
 
});
 
});
8.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
describe("a test", function(){
var foo = false;
var promise;
 
beforeEach(function(){
promise = new WinJS.Promise(function(complete){
 
// complete the promise with a value of false
// so that the expectation fails
complete(false);
 
});
});
 
it("should pass", function(done){
 
promise.then(function(value){
 
expect(value).equals(true);
done();
 
}).done(null, done); // <<-------
 
});
 
});
9.js
JavaScript
1 2 3 4 5 6 7 8
it("should pass", function(done){
promise.then(function(value){
expect(value).equals(true);
done();
 
}).done(done, done); // <<---- FAIL
 
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.