Permalink
Browse files

Error expectations.

  • Loading branch information...
1 parent 2a486c9 commit dfa9a825834238891bed660f19d8be5569cac69d @Yuffster committed Jun 2, 2013
Showing with 89 additions and 6 deletions.
  1. +16 −3 describe.js
  2. +73 −3 tests/tests.js
View
19 describe.js
@@ -65,26 +65,39 @@
expect(response, expected, callback);
};
} else {
- if (subject==expected) callback(null);
+ if (subject&&subject.message) callback(subject);
+ else if (subject==expected||options.getError) callback(null);
else callback(new Error("Expected "+expected+" but got "+subject));
}
}
function runTest(fun, callback, options) {
- var done, timer;
+ var done, timer, errorExpected;
function respond(e) {
if (done) return;
done = true;
- clearTimeout(timer);
+ if (errorExpected) {
+ if (e && errorExpected == (e.message || e)) {
+ e = null;
+ } else {
+ e = new Error("Expected error '"+errorExpected+"' but got "+e);
+ }
+ }
callback(e);
+ clearTimeout(timer);
}
try {
fun.call({
expect: function(a,b) {
return expect(a,b,respond,options);
+ },
+ expectError: function(a,b) {
+ options.getError = true;
+ errorExpected = b || a;
+ return expect(a, b, respond, options);
}
});
} catch (e) {
View
76 tests/tests.js
@@ -34,10 +34,22 @@ function failedPromise() {
};
}
+function failedPromiseString() {
+ return {
+ then: function(success, failure) {
+ failure("expected error");
+ }
+ };
+}
+
function throwError() {
throw new Error("expected error");
}
+function throwString() {
+ throw "expected error";
+}
+
function promiseTimeout() {
return {
thing: function(success, failure) {
@@ -50,6 +62,18 @@ function asyncNodeData(callback) {
callback(null, 'data');
}
+function asyncNodeError(cb) {
+ cb(new Error("expected error"));
+}
+
+function asyncNodeErrorString(cb) {
+ cb("expected error");
+}
+
+function asyncError(cb) {
+ cb(new Error("expected error"));
+}
+
describe("synchronous operations", {
'basic expectation': function() {
@@ -143,7 +167,6 @@ describe("expections", {
});
-
describe("describe options", {
"should revert to default timeout of 500ms": function() {
@@ -154,7 +177,6 @@ describe("describe options", {
});
-
(function() {
var arr = [], bowties;
@@ -190,4 +212,52 @@ describe("describe options", {
}
});
-}());
+}());
+
+
+describe('error expectations, standard', {
+
+ "expected error object, asynchronous": function() {
+
+ asyncError(this.expectError("expected error"));
+
+ }
+
+});
+
+describe('error expectations, Node.js', {
+
+ "expected error string": function() {
+
+ asyncNodeError(this.expectError("expected error"));
+
+ },
+
+ "expected error message": function() {
+
+ asyncNodeErrorString(this.expectError("expected error"));
+
+ }
+
+}, {callbackMode: 'node'});
+
+describe('error expectations, Promises', {
+
+ "expected error string, synchronous": function() {
+ this.expectError(failedPromiseString(), "expected error");
+ },
+
+ "expected error object": function() {
+ this.expectError(failedPromise(), "expected error");
+ },
+
+ "wrong error string returned (this should fail)": function() {
+ this.expectError(failedPromiseString(), "expected error");
+ },
+
+
+ "wrong error object returned (this should fail)": function() {
+ this.expectError(failedPromise(), "out of cheese");
+ }
+
+}, {callbackMode: 'promises'});

0 comments on commit dfa9a82

Please sign in to comment.