Skip to content

Commit

Permalink
Improve the error messages for helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
spadgos committed Sep 8, 2014
1 parent 8616526 commit f82af7d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
18 changes: 13 additions & 5 deletions Tyrtle.js
Expand Up @@ -1508,6 +1508,10 @@ function runHelper(mod, helpers, callback, catchBlock) {
}
}

function getStatusMessageForHelperFailure(helperType, err) {
return "Error in the " + helperType + " helper. " + (err ? err.message || err : "");
}

util.extend(Module.prototype, {
tests : null, // array of tests
tyrtle : null, // reference to the owner Tyrtle instance
Expand Down Expand Up @@ -1646,6 +1650,7 @@ util.extend(Module.prototype, {
if (!test.error) {
++mod.errors;
test.error = e;
test.statusMessage = getStatusMessageForHelperFailure("afterAll", e);
}
}
callback();
Expand Down Expand Up @@ -1691,6 +1696,7 @@ util.extend(Module.prototype, {
for (j = 0, jl = mod.tests.length; j < jl; ++j) {
renderer.get().beforeTest(mod.tests[j], mod, mod.tyrtle);
mod.tests[j].status = FAIL;
mod.tests[j].statusMessage = getStatusMessageForHelperFailure("beforeAll", e);
mod.tests[j].error = e;
renderer.get().afterTest(mod.tests[j], mod, mod.tyrtle);
}
Expand Down Expand Up @@ -1718,15 +1724,15 @@ util.extend(Module.prototype, {
runHelper(mod, mod.helpers.after, callback, function (e) {
test.status = FAIL;
if (!test.error) {
test.statusMessage = "Error in the after helper. " + e.message;
test.statusMessage = getStatusMessageForHelperFailure("after", e);
test.error = e;
}
callback();
});
};
runHelper(this, this.helpers.before, go, function (e) {
test.status = FAIL;
test.statusMessage = "Error in the before helper.";
test.statusMessage = getStatusMessageForHelperFailure("before", e);
test.error = e;
done();
});
Expand Down Expand Up @@ -1779,8 +1785,10 @@ util.extend(Module.prototype, {
};
runHelper(mod, mod.helpers.afterAll, aftersDone, function (e) {
test.status = FAIL;
test.error = e;
test.statusMessage = "Error in the afterAll helper";
if (!test.error) {
test.error = e;
test.statusMessage = getStatusMessageForHelperFailure("afterAll", e.message);
}
aftersDone();
});
});
Expand All @@ -1798,7 +1806,7 @@ util.extend(Module.prototype, {
runHelper(this, this.helpers.beforeAll, run, function (e) {
test.status = FAIL;
test.error = e;
test.statusMessage = "Error in the beforeAll helper";
test.statusMessage = getStatusMessageForHelperFailure("beforeAll", e);
++mod.fails;
++tyrtle.fails;
++mod.errors;
Expand Down
18 changes: 13 additions & 5 deletions src/Module.js
Expand Up @@ -65,6 +65,10 @@ function runHelper(mod, helpers, callback, catchBlock) {
}
}

function getStatusMessageForHelperFailure(helperType, err) {
return "Error in the " + helperType + " helper. " + (err ? err.message || err : "");
}

util.extend(Module.prototype, {
tests : null, // array of tests
tyrtle : null, // reference to the owner Tyrtle instance
Expand Down Expand Up @@ -203,6 +207,7 @@ util.extend(Module.prototype, {
if (!test.error) {
++mod.errors;
test.error = e;
test.statusMessage = getStatusMessageForHelperFailure("afterAll", e);
}
}
callback();
Expand Down Expand Up @@ -248,6 +253,7 @@ util.extend(Module.prototype, {
for (j = 0, jl = mod.tests.length; j < jl; ++j) {
renderer.get().beforeTest(mod.tests[j], mod, mod.tyrtle);
mod.tests[j].status = FAIL;
mod.tests[j].statusMessage = getStatusMessageForHelperFailure("beforeAll", e);
mod.tests[j].error = e;
renderer.get().afterTest(mod.tests[j], mod, mod.tyrtle);
}
Expand Down Expand Up @@ -275,15 +281,15 @@ util.extend(Module.prototype, {
runHelper(mod, mod.helpers.after, callback, function (e) {
test.status = FAIL;
if (!test.error) {
test.statusMessage = "Error in the after helper. " + e.message;
test.statusMessage = getStatusMessageForHelperFailure("after", e);
test.error = e;
}
callback();
});
};
runHelper(this, this.helpers.before, go, function (e) {
test.status = FAIL;
test.statusMessage = "Error in the before helper.";
test.statusMessage = getStatusMessageForHelperFailure("before", e);
test.error = e;
done();
});
Expand Down Expand Up @@ -336,8 +342,10 @@ util.extend(Module.prototype, {
};
runHelper(mod, mod.helpers.afterAll, aftersDone, function (e) {
test.status = FAIL;
test.error = e;
test.statusMessage = "Error in the afterAll helper";
if (!test.error) {
test.error = e;
test.statusMessage = getStatusMessageForHelperFailure("afterAll", e.message);
}
aftersDone();
});
});
Expand All @@ -355,7 +363,7 @@ util.extend(Module.prototype, {
runHelper(this, this.helpers.beforeAll, run, function (e) {
test.status = FAIL;
test.error = e;
test.statusMessage = "Error in the beforeAll helper";
test.statusMessage = getStatusMessageForHelperFailure("beforeAll", e);
++mod.fails;
++tyrtle.fails;
++mod.errors;
Expand Down
33 changes: 22 additions & 11 deletions test/helpers.js
Expand Up @@ -53,20 +53,22 @@ asyncTest("Test helpers are executed properly", function () {
});
asyncTest("Errors in the before are reported on each test", function () {
var t;
expect(3);
expect(5);
t = new Tyrtle({
callback : function () {
equal(this.passes, 2, "two should have passed.");
equal(this.fails, 2, "two should have failed.");
equal(this.errors, 2, "those two failures should have been from errors.");
equal(this.modules[0].tests[0].statusMessage, "Error in the before helper. error 1");
equal(this.modules[0].tests[2].statusMessage, "Error in the before helper. error 3");
start();
}
});
t.module("foo", function () {
var x = 0;
this.before(function () {
if (++x % 2) {
throw "an error";
throw "error " + x;
}
});
this.test("a", function () {});
Expand All @@ -78,20 +80,22 @@ asyncTest("Errors in the before are reported on each test", function () {
});
asyncTest("Errors in the after are reported on each test", function () {
var t;
expect(3);
expect(5);
t = new Tyrtle({
callback : function () {
equal(this.passes, 2, "two should have passed.");
equal(this.fails, 2, "two should have failed.");
equal(this.errors, 2, "those two failures should have been from errors.");
equal(this.modules[0].tests[0].statusMessage, "Error in the after helper. error 1");
equal(this.modules[0].tests[2].statusMessage, "Error in the after helper. error 3");
start();
}
});
t.module("foo", function () {
var x = 0;
this.after(function () {
if (++x % 2) {
throw "an error";
throw new Error("error " + x);
}
});
this.test("a", function () {});
Expand All @@ -103,13 +107,17 @@ asyncTest("Errors in the after are reported on each test", function () {
});
asyncTest("Errors in the beforeAll are reported on all tests", function () {
var t, count = 0;
expect(4);
expect(8);
t = new Tyrtle({
callback : function () {
equal(count, 0, "No tests should have been actually executed");
equal(this.passes, 0, "none should have passed.");
equal(this.fails, 4, "all should have failed.");
equal(this.errors, 4, "those failures should have been from errors.");
equal(this.modules[0].tests[0].statusMessage, "Error in the beforeAll helper. an error");
equal(this.modules[0].tests[1].statusMessage, "Error in the beforeAll helper. an error");
equal(this.modules[0].tests[2].statusMessage, "Error in the beforeAll helper. an error");
equal(this.modules[0].tests[3].statusMessage, "Error in the beforeAll helper. an error");
start();
}
});
Expand Down Expand Up @@ -146,13 +154,14 @@ asyncTest("Errors in the beforeAll are reported on all tests", function () {
shouldSkip = false;
shouldPass = true;
var t;
expect(4);
expect(5);
t = new Tyrtle({
callback : function () {
equal(this.passes, 3, "the first three should have passed.");
equal(this.fails, 1, "the last should have failed.");
equal(this.errors, 1, "that failure should have been from an error.");
equal(this.skips, 0, "none should have skipped");
equal(this.modules[0].tests[3].statusMessage, "Error in the afterAll helper. an error");
start();
}
});
Expand All @@ -162,13 +171,14 @@ asyncTest("Errors in the beforeAll are reported on all tests", function () {
asyncTest("Errors in the afterAll are reported on the last test, even when it is skipped", function () {
shouldSkip = true;
var t;
expect(4);
expect(5);
t = new Tyrtle({
callback : function () {
equal(this.passes, 3, "the first three should have passed.");
equal(this.fails, 1, "the last should have failed.");
equal(this.errors, 1, "that failure should have been from an error.");
equal(this.skips, 0, "none should have skipped.");
equal(this.modules[0].tests[3].statusMessage, "Error in the afterAll helper. an error");
start();
}
});
Expand All @@ -178,14 +188,14 @@ asyncTest("Errors in the beforeAll are reported on all tests", function () {
asyncTest("Errors in the afterAll are reported on the last test, even when it has already failed", function () {
shouldSkip = false;
shouldPass = false;
var t;
expect(4);
t = new Tyrtle({
expect(5);
var t = new Tyrtle({
callback : function () {
equal(this.passes, 3, "the first three should have passed.");
equal(this.fails, 1, "the last should have failed.");
equal(this.errors, 1, "that failure should have been from an error.");
equal(this.skips, 0, "none should have skipped.");
equal(this.modules[0].tests[3].statusMessage, "Error in the afterAll helper. an error");
start();
}
});
Expand All @@ -199,13 +209,14 @@ asyncTest("Errors in the before/afterAll are handled when rerunning tests", func
t = new Tyrtle({
callback : function () {
var mod = t.modules[0],
test = mod.tests[0]
test = mod.tests[0]
;
equal(t.passes, 1);
afterError = 'abc';
mod.rerunTest(test, t, function () {
equal(t.fails, 1);
equal(t.errors, 1);
debugger;
equal(test.error, 'abc');
beforeError = 'def';
mod.rerunTest(test, t, function () {
Expand Down

0 comments on commit f82af7d

Please sign in to comment.