Comparing version 1.1.2 to 1.1.3
@@ -5,6 +5,20 @@ 'use strict'; | ||
if (! global.Esquire) throw new Error("Esquire not available"); | ||
var successInstance = {}; | ||
var successPromise = function() { | ||
return this.then(function(success) { | ||
return successInstance; | ||
}) | ||
}; | ||
global.Esquire.$$Promise.prototype.done = successPromise; | ||
if (global.Promise) global.Promise.prototype.done = successPromise; | ||
/* Extension to mocha: "promises(...)" is like a deferred "it(...)" */ | ||
function invoke(itfn, title, fn) { | ||
return itfn.call(this, title, function(done) { | ||
var failure; | ||
var failure = null; | ||
try { | ||
@@ -14,3 +28,5 @@ var promise = fn.call(this); | ||
promise.then(function(success) { | ||
done(failure); | ||
if (failure != null) done(failure); | ||
else if (success === successInstance) done(); | ||
else done(new Error("Not notified of completion: call 'done()' on the last promise")); | ||
}, function(failure) { | ||
@@ -20,2 +36,5 @@ console.warn("Rejected: ", failure); | ||
}) | ||
} else if (promise === successPromise) { | ||
done(new Error("The completion notification 'done()' must be called as a function")); | ||
done(); | ||
} else { | ||
@@ -22,0 +41,0 @@ done(new Error("Test did not return a Promise")); |
{ | ||
"name": "esquire", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"main": "node-adapter.js", | ||
@@ -5,0 +5,0 @@ "description": "Esquire: asynchronous injection framework", |
@@ -23,2 +23,5 @@ 'use strict'; | ||
/* Promise implementation */ | ||
var Promise = global.Promise || PromiseImpl; | ||
/*==========================================================================* | ||
@@ -115,3 +118,3 @@ | *======================================================================* | | ||
"promise": { enumerable: true, configurable: false, value: | ||
Object.defineProperties(new Object(), { | ||
Object.defineProperties(Object.create(Promise.prototype), { | ||
"then": { enumerable: true, configurable: false, value: function(onSuccess, onFailure) { | ||
@@ -642,5 +645,2 @@ var chained = new Deferring(onSuccess, onFailure); | ||
/* Promise implementation */ | ||
var Promise = global.Promise || PromiseImpl; | ||
/* Listeners (should contain only "define") */ | ||
@@ -647,0 +647,0 @@ var listeners = { define: [] }; |
@@ -14,3 +14,3 @@ 'use strict'; | ||
/* Always test our "emulated" and default Promise implementation */ | ||
createTests("with emulated Promize", Esquire.$$Promise); | ||
createTests("with emulated Promise", Esquire.$$Promise); | ||
@@ -25,3 +25,3 @@ /* | ||
it("should resolve a deferred", function(done) { | ||
promises("should resolve a deferred", function() { | ||
@@ -32,7 +32,5 @@ var deferred = new Deferred(); | ||
promise.then(wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
expect(result).to.be.equal("foo"); | ||
}), function(result) { | ||
done(result || new Error("Failed")); | ||
}); | ||
}).done(); | ||
@@ -43,3 +41,3 @@ }); | ||
it("should resolve a deferred resolved with a resolved promise", function(done) { | ||
promises("should resolve a deferred resolved with a resolved promise", function() { | ||
@@ -50,7 +48,5 @@ var deferred = new Deferred(); | ||
promise.then(wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
expect(result).to.be.equal("foo"); | ||
}), function(result) { | ||
done(result || new Error("Failed")); | ||
}); | ||
}).done(); | ||
@@ -61,3 +57,3 @@ }); | ||
it("should reject a deferred", function(done) { | ||
promises("should reject a deferred", function() { | ||
@@ -68,7 +64,7 @@ var deferred = new Deferred(); | ||
promise.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.equal("foo"); | ||
})); | ||
}).done(); | ||
@@ -79,3 +75,3 @@ }); | ||
it("should reject a deferred resolved with a rejected promise", function(done) { | ||
promises("should reject a deferred resolved with a rejected promise", function() { | ||
@@ -86,7 +82,7 @@ var deferred = new Deferred(); | ||
promise.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.equal("foo"); | ||
})); | ||
}).done(); | ||
@@ -97,3 +93,3 @@ }); | ||
it("should handle deep exceptions", function(done) { | ||
promises("should handle deep exceptions", function() { | ||
@@ -123,7 +119,7 @@ var deferred = new Deferred(); | ||
promise4.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise4.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.equal("failure=bar"); | ||
})); | ||
}).done(); | ||
@@ -134,3 +130,3 @@ }); | ||
it("should resolve deferred resolved with undefined", function(done) { | ||
promises("should resolve deferred resolved with undefined", function() { | ||
@@ -154,3 +150,3 @@ var deferred1 = new Deferred(); | ||
PromiseImpl.all([promise1, promise2]).then(function(success) { | ||
return PromiseImpl.all([promise1, promise2]).then(function(success) { | ||
expect(success).to.be.instanceof(Array); | ||
@@ -160,10 +156,4 @@ expect(success.length).to.equal(2); | ||
expect(success[1]).to.equal(undefined); | ||
}) | ||
}).done(); | ||
.then(function(success) { | ||
done(); | ||
}, function(failure) { | ||
done(failure); | ||
}); | ||
}); | ||
@@ -173,3 +163,3 @@ | ||
promises("should timeout with no rejection specified", function(done) { | ||
promises("should timeout with no rejection specified", function() { | ||
@@ -182,8 +172,8 @@ var deferred = new Deferred(10); | ||
return deferred.promise.then(wrap(done, function(result) { | ||
return deferred.promise.then(function(result) { | ||
throw new Error("Should not resolve"); | ||
}), function(failure) { | ||
}, function(failure) { | ||
expect(failure).to.be.instanceof(Error); | ||
expect(failure.message).to.be.equal('Timeout waiting for resolution/rejection'); | ||
}) | ||
}).done(); | ||
@@ -194,3 +184,3 @@ }); | ||
promises("should timeout with a rejection message", function(done) { | ||
promises("should timeout with a rejection message", function() { | ||
@@ -204,8 +194,8 @@ // 0, not null but also not truthy... | ||
return deferred.promise.then(wrap(done, function(result) { | ||
return deferred.promise.then(function(result) { | ||
throw new Error("Should not resolve"); | ||
}), function(failure) { | ||
}, function(failure) { | ||
expect(failure).to.be.instanceof(Error); | ||
expect(failure.message).to.be.equal('Fail, deferred, FAIL!'); | ||
}) | ||
}).done(); | ||
@@ -216,3 +206,3 @@ }); | ||
promises("should timeout with a specific rejection", function(done) { | ||
promises("should timeout with a specific rejection", function() { | ||
@@ -226,8 +216,8 @@ // 0, not null but also not truthy... | ||
return deferred.promise.then(wrap(done, function(result) { | ||
return deferred.promise.then(function(result) { | ||
throw new Error("Should not resolve"); | ||
}), function(failure) { | ||
}, function(failure) { | ||
expect(failure).to.be.a('number'); | ||
expect(failure).to.be.equal(0); | ||
}) | ||
}).done(); | ||
@@ -240,17 +230,2 @@ }); | ||
/* ======================================================================== */ | ||
/* WRAP: Wrap a done and a function for easy testing */ | ||
/* ======================================================================== */ | ||
function wrap(done, what) { | ||
return(function() { | ||
try { | ||
what.apply(this, arguments); | ||
done(); | ||
} catch (error) { | ||
done(error); | ||
} | ||
}) | ||
} | ||
})((function() { | ||
@@ -257,0 +232,0 @@ try { |
@@ -48,2 +48,4 @@ 'use strict'; | ||
.done(); | ||
}); | ||
@@ -66,2 +68,4 @@ | ||
.done(); | ||
}); | ||
@@ -85,2 +89,4 @@ | ||
.done(); | ||
}); | ||
@@ -104,2 +110,4 @@ | ||
.done(); | ||
}); | ||
@@ -123,2 +131,4 @@ | ||
.done(); | ||
}) | ||
@@ -142,2 +152,4 @@ | ||
.done(); | ||
}) | ||
@@ -153,2 +165,3 @@ | ||
}) | ||
.done(); | ||
@@ -165,2 +178,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -177,2 +191,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -188,2 +203,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -199,2 +215,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -209,2 +226,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -226,2 +244,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -241,2 +260,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -256,2 +276,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -271,2 +292,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -283,2 +305,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -295,2 +318,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -315,2 +339,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -330,2 +355,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -342,2 +368,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -356,2 +383,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -374,3 +402,4 @@ }); | ||
expect(e0).not.to.be.equal(e1); | ||
}); | ||
}) | ||
.done(); | ||
@@ -393,2 +422,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -412,4 +442,4 @@ }); | ||
}) | ||
.done(); | ||
}); | ||
@@ -434,2 +464,3 @@ | ||
}) | ||
.done(); | ||
@@ -466,2 +497,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -487,2 +519,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -508,2 +541,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -529,2 +563,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -556,2 +591,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -582,2 +618,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -608,2 +645,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -634,2 +672,3 @@ }); | ||
}) | ||
.done(); | ||
@@ -677,3 +716,3 @@ }); | ||
expect(Object.keys(dependencies).length).to.be.equal(0); | ||
}); | ||
}).done(); | ||
}); | ||
@@ -688,3 +727,3 @@ | ||
expect(dependencies['module-b']).to.equal(Esquire.modules['module-b']); | ||
}); | ||
}).done(); | ||
}); | ||
@@ -698,3 +737,3 @@ | ||
expect(dependencies['module-c']).to.equal(Esquire.modules['module-c']); | ||
}); | ||
}).done(); | ||
}); | ||
@@ -709,3 +748,3 @@ | ||
expect(dependencies['module-d']).to.equal(Esquire.modules['module-d']); | ||
}); | ||
}).done(); | ||
}); | ||
@@ -721,3 +760,3 @@ | ||
expect(dependencies['module-c']).to.equal(Esquire.modules['module-c']); | ||
}); | ||
}).done(); | ||
}); | ||
@@ -734,3 +773,3 @@ | ||
expect(dependencies['module-d']).to.equal(Esquire.modules['module-d']); | ||
}); | ||
}).done(); | ||
}); | ||
@@ -768,3 +807,3 @@ | ||
expect(dependencies['module-d']).to.equal(Esquire.modules['module-d']); | ||
}); | ||
}).done(); | ||
}); | ||
@@ -785,3 +824,3 @@ | ||
expect(dependencies['circular-g']).to.equal(Esquire.modules['circular-g']); | ||
}); | ||
}).done(); | ||
}); | ||
@@ -795,3 +834,3 @@ | ||
expect(dependencies['circular-self']).to.equal(Esquire.modules['circular-self']); | ||
}); | ||
}).done(); | ||
}); | ||
@@ -813,3 +852,3 @@ | ||
expect(value).to.be.equal("value for " + name); | ||
}); | ||
}).done(); | ||
}); | ||
@@ -836,3 +875,3 @@ | ||
expect(invoked).to.be.false; | ||
}) | ||
}).done() | ||
@@ -864,3 +903,3 @@ }); | ||
expect(invoked).to.be.false; | ||
}) | ||
}).done(); | ||
@@ -877,3 +916,3 @@ }); | ||
expect(failure.message).to.match(/module 'not-known' not found/i); | ||
}) | ||
}).done(); | ||
@@ -897,3 +936,3 @@ }); | ||
expect(failure.originalCause.message).to.equal("Esquire: Detected circular dependency in circular-a -> circular-b -> circular-c -> circular-d -> circular-e -> circular-f -> circular-g -> circular-a"); | ||
}) | ||
}).done(); | ||
@@ -917,3 +956,3 @@ }); | ||
expect(failure.originalCause.message).to.equal("Esquire: Detected circular dependency in circular-self -> circular-self"); | ||
}) | ||
}).done(); | ||
@@ -920,0 +959,0 @@ }); |
@@ -20,3 +20,3 @@ 'use strict'; | ||
expect(result).to.equal(global.console); | ||
}) | ||
}).done(); | ||
@@ -30,3 +30,3 @@ }); | ||
expect(result).to.be.an('undefined'); | ||
}) | ||
}).done(); | ||
@@ -62,3 +62,3 @@ }); | ||
expect(t3).to.be.a('undefined'); | ||
}) | ||
}).done(); | ||
@@ -96,3 +96,3 @@ }); | ||
delete global.prefixedTest; | ||
}) | ||
}).done(); | ||
@@ -123,3 +123,3 @@ }); | ||
it('should override and cache globals with proper definition', function() { | ||
promises('should override and cache globals with proper definition', function() { | ||
@@ -155,3 +155,3 @@ var count = 0; | ||
expect(t3).to.be.equal(expected); | ||
}) | ||
}).done(); | ||
@@ -179,3 +179,3 @@ }); | ||
expect(result[0]).to.equal(result[1]) | ||
}); | ||
}).done(); | ||
@@ -182,0 +182,0 @@ }); |
@@ -16,101 +16,89 @@ 'use strict'; | ||
it('should resolve with no scripts', function(done) { | ||
Esquire.load() | ||
promises('should resolve with no scripts', function() { | ||
return Esquire.load() | ||
.then(function(success) { | ||
done(); | ||
}, function(failure) { | ||
done(failure); | ||
}); | ||
expect(success).to.deep.equal(Esquire.modules); | ||
}).done(); | ||
}) | ||
it('should resolve with an empty script', function(done) { | ||
promises('should resolve with an empty script', function() { | ||
Esquire.load(file("empty-script.js")) | ||
return Esquire.load(file("empty-script.js")) | ||
.then(function(success) { | ||
done(); | ||
}, function(failure) { | ||
done(failure); | ||
}); | ||
expect(success).to.deep.equal(Esquire.modules); | ||
}).done(); | ||
}) | ||
it('should resolve with a normal script', function(done) { | ||
promises('should resolve with a normal script', function() { | ||
Esquire.load(file("normal-script.js")) | ||
return Esquire.load(file("normal-script.js")) | ||
.then(function(success) { | ||
done(); | ||
}, function(failure) { | ||
done(failure); | ||
}); | ||
expect(success).to.deep.equal(Esquire.modules); | ||
}).done(); | ||
}) | ||
it('should reject on a non existant script', function(done) { | ||
promises('should reject on a non existant script', function() { | ||
Esquire.load(file("non-existant.js")) | ||
return Esquire.load(file("non-existant.js")) | ||
.then(function(success) { | ||
done(new Error("Should fail")); | ||
throw new Error("Should fail"); | ||
}, function(failure) { | ||
done(); | ||
}); | ||
expect(failure).to.be.instanceof(Error); | ||
}).done(); | ||
}) | ||
it('should reject on parse exception', function(done) { | ||
promises('should reject on parse exception', function() { | ||
Esquire.load(file("parse-exception.js")) | ||
return Esquire.load(file("parse-exception.js")) | ||
.then(function(success) { | ||
done(new Error("Should fail")); | ||
throw new Error("Should fail"); | ||
}, function(failure) { | ||
done(); | ||
}); | ||
expect(failure).to.be.instanceof(Error); | ||
}).done(); | ||
}) | ||
it('should reject on undefined variable', function(done) { | ||
promises('should reject on undefined variable', function() { | ||
Esquire.load(file("undefined-variable.js")) | ||
return Esquire.load(file("undefined-variable.js")) | ||
.then(function(success) { | ||
done(new Error("Should fail")); | ||
throw new Error("Should fail"); | ||
}, function(failure) { | ||
done(); | ||
}); | ||
expect(failure).to.be.instanceof(Error); | ||
}).done(); | ||
}) | ||
it('should reject on exception thrown', function(done) { | ||
promises('should reject on exception thrown', function() { | ||
Esquire.load(file("throws-error.js")) | ||
return Esquire.load(file("throws-error.js")) | ||
.then(function(success) { | ||
done(new Error("Should fail")); | ||
throw new Error("Should fail"); | ||
}, function(failure) { | ||
done(); | ||
}); | ||
expect(failure).to.be.instanceof(Error); | ||
}).done(); | ||
}) | ||
it('should reject on multiple files', function(done) { | ||
promises('should reject on multiple files', function() { | ||
Esquire.load(file("empty-script.js"), file("throws-error.js")) | ||
return Esquire.load(file("empty-script.js"), file("throws-error.js")) | ||
.then(function(success) { | ||
done(new Error("Should fail")); | ||
throw new Error("Should fail"); | ||
}, function(failure) { | ||
done(); | ||
}); | ||
expect(failure).to.be.instanceof(Error); | ||
}).done(); | ||
}) | ||
it('should resolve on multiple files', function(done) { | ||
promises('should resolve on multiple files', function() { | ||
Esquire.load(file("empty-script.js"), file("normal-script.js")) | ||
return Esquire.load(file("empty-script.js"), file("normal-script.js")) | ||
.then(function(success) { | ||
done(); | ||
}, function(failure) { | ||
done(failure); | ||
}); | ||
expect(success).to.deep.equal(Esquire.modules); | ||
}).done(); | ||
}) | ||
it('should load a module from an external script', function(done) { | ||
promises('should load a module from an external script', function() { | ||
Esquire.load(file("module-script.js")) | ||
return Esquire.load(file("module-script.js")) | ||
.then(function(success) { | ||
expect(success['loaded-module']).to.exist(); | ||
return new Esquire().require('loaded-module'); | ||
}, function(failure) { | ||
console.warn("Exception loading script 'module-script.js"); | ||
done(failure); | ||
}) | ||
@@ -120,16 +108,8 @@ | ||
expect(module).to.be.equal('This was loaded from an external module'); | ||
done(); | ||
}, function(failure) { | ||
console.warn("Exception resolving module from 'module-script.js"); | ||
done(failure); | ||
}) | ||
.then(function(success) { | ||
done(); | ||
}, function(failure) { | ||
done(failure); | ||
}); | ||
.done(); | ||
}) | ||
}) |
@@ -42,3 +42,3 @@ 'use strict'; | ||
it("should resolve 1", function(done) { | ||
promises("should resolve 1", function() { | ||
@@ -49,7 +49,5 @@ var promise = new PromiseImpl(function(resolve, reject) { | ||
promise.then(wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
expect(result).to.be.equal("foo"); | ||
}), function(result) { | ||
done(result || new Error("Failed")); | ||
}); | ||
}).done(); | ||
@@ -60,11 +58,9 @@ }); | ||
it("should resolve 2", function(done) { | ||
promises("should resolve 2", function() { | ||
var promise = PromiseImpl.resolve("foo"); | ||
promise.then(wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
expect(result).to.be.equal('foo'); | ||
}), function(result) { | ||
done(result || new Error("Failed")); | ||
}); | ||
}).done(); | ||
@@ -75,3 +71,3 @@ }); | ||
it("should resolve a resolved promise", function(done) { | ||
promises("should resolve a resolved promise", function() { | ||
@@ -81,7 +77,5 @@ var resolved = PromiseImpl.resolve("foo"); | ||
promise.then(wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
expect(result).to.be.equal('foo'); | ||
}), function(result) { | ||
done(result || new Error("Failed")); | ||
}); | ||
}).done(); | ||
@@ -95,3 +89,3 @@ | ||
it("should reject 1", function(done) { | ||
promises("should reject 1", function() { | ||
@@ -102,7 +96,7 @@ var promise = new PromiseImpl(function(resolve, reject) { | ||
promise.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.equal("foo"); | ||
})); | ||
}).done(); | ||
@@ -113,11 +107,11 @@ }); | ||
it("should reject 2", function(done) { | ||
promises("should reject 2", function() { | ||
var promise = PromiseImpl.reject("foo"); | ||
promise.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.equal("foo"); | ||
})); | ||
}).done(); | ||
@@ -128,3 +122,3 @@ }); | ||
it("should reject a rejected promise", function(done) { | ||
promises("should reject a rejected promise", function() { | ||
@@ -134,7 +128,7 @@ var rejected = PromiseImpl.reject("foo"); | ||
promise.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.equal("foo"); | ||
})); | ||
}).done(); | ||
@@ -147,3 +141,3 @@ }); | ||
it("should handle chaining on resolve", function(done) { | ||
promises("should handle chaining on resolve", function() { | ||
@@ -154,11 +148,7 @@ var promise = new PromiseImpl(function(resolve, reject) { | ||
return result + "bar"; | ||
}, function(result) { | ||
done(result || new Error("Failed")); | ||
}); | ||
promise.then(wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
expect(result).to.be.equal('foobar'); | ||
}), function(result) { | ||
done(result || new Error("Failed")); | ||
}); | ||
}).done(); | ||
@@ -169,3 +159,3 @@ }); | ||
it("should handle chaining on reject", function(done) { | ||
promises("should handle chaining on reject", function() { | ||
@@ -175,3 +165,3 @@ var promise = new PromiseImpl(function(resolve, reject) { | ||
}).then(function(result) { | ||
done(result || new Error("Failed")); | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
@@ -181,7 +171,5 @@ return result + "bar"; | ||
promise.then(wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
expect(result).to.be.equal('foobar'); | ||
}), function(result) { | ||
done(result || new Error("Failed")); | ||
}); | ||
}).done(); | ||
@@ -192,3 +180,3 @@ }); | ||
it("should handle exceptions chaining on resolve", function(done) { | ||
promises("should handle exceptions chaining on resolve", function() { | ||
@@ -199,11 +187,9 @@ var promise = new PromiseImpl(function(resolve, reject) { | ||
throw "success=" + result; | ||
}, function(result) { | ||
done(result || new Error("Failed")); | ||
}); | ||
promise.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.equal("success=foo"); | ||
})); | ||
}).done(); | ||
@@ -214,3 +200,3 @@ }); | ||
it("should handle exceptions chaining on reject", function(done) { | ||
promises("should handle exceptions chaining on reject", function() { | ||
@@ -220,3 +206,3 @@ var promise = new PromiseImpl(function(resolve, reject) { | ||
}).then(function(result) { | ||
done(result || new Error("Failed")); | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
@@ -226,7 +212,7 @@ throw "failure=" + result; | ||
promise.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.equal("failure=foo"); | ||
})); | ||
}).done(); | ||
@@ -237,3 +223,3 @@ }); | ||
it("should handle deep exceptions", function(done) { | ||
promises("should handle deep exceptions", function() { | ||
@@ -262,7 +248,7 @@ var first = new PromiseImpl(function(resolve, reject) { | ||
promise4.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise4.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.equal("failure=bar"); | ||
})); | ||
}).done(); | ||
@@ -275,7 +261,7 @@ }); | ||
it("should resolve all promises with undefined results", function(done) { | ||
promises("should resolve all promises with undefined results", function() { | ||
var p1 = new PromiseImpl(function (resolve, reject) { resolve("defined") }); | ||
var p2 = new PromiseImpl(function (resolve, reject) { resolve() }); | ||
PromiseImpl.all([p1, p2]).then(function(success) { | ||
return PromiseImpl.all([p1, p2]).then(function(success) { | ||
expect(success).to.be.instanceof(Array); | ||
@@ -285,12 +271,7 @@ expect(success.length).to.equal(2); | ||
expect(success[1]).to.equal(undefined); | ||
}) | ||
}).done() | ||
.then(function(success) { | ||
done(); | ||
}, function(failure) { | ||
done(failure); | ||
}); | ||
}); | ||
it("should not throw exceptions", function(done) { | ||
promises("should not throw exceptions", function() { | ||
@@ -301,65 +282,61 @@ var promise = new PromiseImpl(function(resolve, reject) { | ||
promise.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.equal("foo"); | ||
})); | ||
}).done(); | ||
}); | ||
it("should resolve an empty array of promises", function(done) { | ||
promises("should resolve an empty array of promises", function() { | ||
var promise = PromiseImpl.all([]); | ||
promise.then(wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
expect(result).to.be.eql([]); | ||
}), function(result) { | ||
done(result || new Error("Failed")); | ||
}); | ||
}).done(); | ||
}); | ||
it("should combine multiple resolved promises", function(done) { | ||
promises("should combine multiple resolved promises", function() { | ||
var promise = PromiseImpl.all(["foo", PromiseImpl.resolve("bar"), PromiseImpl.resolve("baz")]); | ||
promise.then(wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
expect(result).to.be.eql(['foo', 'bar', 'baz']); | ||
}), function(result) { | ||
done(result || new Error("Failed")); | ||
}); | ||
}).done(); | ||
}); | ||
it("should combine multiple rejected promises", function(done) { | ||
promises("should combine multiple rejected promises", function() { | ||
var promise = PromiseImpl.all(["foo", PromiseImpl.resolve("bar"), PromiseImpl.reject("baz")]); | ||
promise.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.equal('baz'); | ||
})); | ||
}).done(); | ||
}); | ||
it("should fail when all is called with undefined", function(done) { | ||
promises("should fail when all is called with undefined", function() { | ||
PromiseImpl.all().then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return PromiseImpl.all().then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.instanceof(TypeError); | ||
})); | ||
}).done(); | ||
}); | ||
it("should fail when all is called with something other than an array", function(done) { | ||
promises("should fail when all is called with something other than an array", function() { | ||
var promise = PromiseImpl.all("hello"); | ||
promise.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.instanceof(TypeError); | ||
})); | ||
}).done(); | ||
}); | ||
it("should race multiple promises with a winning resolution", function(done) { | ||
promises("should race multiple promises with a winning resolution", function() { | ||
@@ -380,11 +357,9 @@ var resolvedPromiseImpl = new PromiseImpl(function(resolve, reject) { | ||
promise.then(wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
expect(result).to.be.equal('foo'); | ||
}), function(result) { | ||
done(result || new Error("Failed")); | ||
}); | ||
}).done(); | ||
}); | ||
it("should race multiple promises with a winning rejection", function(done) { | ||
promises("should race multiple promises with a winning rejection", function() { | ||
@@ -405,27 +380,27 @@ var resolvedPromiseImpl = new PromiseImpl(function(resolve, reject) { | ||
promise.then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return promise.then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.equal('bar'); | ||
})); | ||
}).done(); | ||
}); | ||
it("should not race an undefined array", function(done) { | ||
promises("should not race an undefined array", function() { | ||
PromiseImpl.race().then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return PromiseImpl.race().then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.instanceof(TypeError); | ||
})); | ||
}).done(); | ||
}); | ||
it("should not race an something other than array", function(done) { | ||
promises("should not race an something other than array", function() { | ||
PromiseImpl.race('foo').then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
return PromiseImpl.race('foo').then(function(result) { | ||
throw (result || new Error("Failed")); | ||
}, function(result) { | ||
expect(result).to.be.instanceof(TypeError); | ||
})); | ||
}).done(); | ||
@@ -436,16 +411,10 @@ }); | ||
new PromiseImpl(function(resolve, reject) { | ||
/* Race the empty array */ | ||
PromiseImpl.race([]).then(function(result) { | ||
done(result || new Error("Failed")); | ||
}, wrap(done, function(result) { | ||
expect(result).to.be.instanceof(TypeError); | ||
done(); | ||
})); | ||
/* Confirm after a bit... */ | ||
setTimeout(function() { done() }, 100); | ||
/* Race the empty array */ | ||
PromiseImpl.race([]).then(function(success) { | ||
done(success || new Error("Failed resolve")); | ||
}, function(failure) { | ||
done(failure || new Error("Failed reject")); | ||
}); | ||
global.setTimeout(done, 50); | ||
@@ -458,17 +427,2 @@ }); | ||
/* ======================================================================== */ | ||
/* WRAP: Wrap a done and a function for easy testing */ | ||
/* ======================================================================== */ | ||
function wrap(done, what) { | ||
return(function() { | ||
try { | ||
what.apply(this, arguments); | ||
done(); | ||
} catch (error) { | ||
done(error); | ||
} | ||
}) | ||
} | ||
})((function() { | ||
@@ -475,0 +429,0 @@ try { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
184387
3376