Created
March 24, 2012 20:24
-
-
Save groner/2187487 to your computer and use it in GitHub Desktop.
Jasmine "eventually" matchers
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
function setupEventually() { | |
// expect(promise).eventually.toEqual(...) | |
// uses Q.when() | |
// how to specify errbacks? override eventually.toThrow? | |
// specs to write: | |
// it('should resolve promises') | |
// it('should timeout') | |
// it('should propagate errors') | |
// it('should proxy other matchers') | |
// it('should proxy other matchers defined later') | |
// it('should not be synchronous') | |
// it('should not stack timeouts') | |
this.getEventualMatchersClass_ = function() { | |
if (this.eventualMatchersClass == null) { | |
var parent = this.getMatchersClass_(), | |
eventualMatchersClass = function() { | |
parent.apply(this, arguments); | |
}; | |
jasmine.util.inherit(eventualMatchersClass, parent); | |
for (var k in parent.prototype) { | |
if (parent.prototype[k] instanceof Function && k != 'report') | |
eventualMatchersClass.prototype[k] = eventualize(parent.prototype[k]); | |
} | |
this.eventualMatchersClass = eventualMatchersClass; | |
} | |
return this.eventualMatchersClass; | |
}; | |
var addMatchers = this.addMatchers; | |
this.addMatchers = function(prototype) { | |
var parent = this.getEventualMatchersClass_(); | |
addMatchers.apply(this, arguments); | |
var eventualMatchersClass = function() { | |
parent.apply(this, arguments); | |
}; | |
jasmine.util.inherit(eventualMatchersClass, parent); | |
for (var k in prototype) { | |
if (prototype[k] instanceof Function && k != 'report') | |
eventualMatchersClass.prototype[k] = eventualize(prototype[k]); | |
} | |
this.eventualMatchersClass = eventualMatchersClass; | |
}; | |
function eventualize(matcher) { | |
return function(expected) { | |
var self = this, | |
args = arguments, | |
done; | |
Q.when(self.actual) | |
.then(function(r) { | |
done = true; | |
matcher.apply(self.spec.expect(r), args); | |
}, function(e) { | |
done = true; | |
self.spec.fail(e); | |
}); | |
self.spec.after(function() { | |
if (done) | |
return | |
// FIXME: this isn't right, we really need one timeout for all | |
self.spec.waitsFor(function() { return done }); | |
}); | |
return true | |
} | |
} | |
var expect = this.expect; | |
this.expect = function(actual) { | |
var immediately = expect.apply(this, arguments); | |
immediately.eventually = new (this.getEventualMatchersClass_())(this.env, actual, this); | |
immediately.eventually.not = new (this.getEventualMatchersClass_())(this.env, actual, this, true); | |
return immediately; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment