Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Commit

Permalink
testContext.compile is now promise-aware
Browse files Browse the repository at this point in the history
  - Async test contexts are promise objects. Keep them as promises
    that resolve with compiled contexts
  - Properly fixes async test cases/specs and runner timing issues
  • Loading branch information
Christian Johansen committed Apr 8, 2012
1 parent 665e1e2 commit 98cc7e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/buster-test/test-context.js
@@ -1,16 +1,19 @@
if (typeof module === "object" && typeof require === "function") {
var buster = require("buster-core");
var when = require("when");
}

buster.testContext = (function () {
var bctx;

function empty(context) {
return context.tests.length == 0 &&
context.contexts.length == 0;
}

function filterContexts(contexts, filter, prefix) {
return reduce(contexts, [], function (filtered, context) {
var ctx = buster.testContext.filter(context, filter, prefix);
var ctx = bctx.filter(context, filter, prefix);
if (ctx.tests.length > 0 || ctx.contexts.length > 0) {
filtered.push(ctx);
}
Expand Down Expand Up @@ -44,14 +47,21 @@ buster.testContext = (function () {
if (!context.tests && typeof context.parse == "function") {
return context.parse();
}

return context;
}

function compile(contexts, filter) {
return reduce(contexts, [], function (compiled, ctx) {
ctx = buster.testContext.filter(parse(ctx), filter);
if (!empty(ctx)) compiled.push(ctx);
if (when.isPromise(ctx)) {
var deferred = when.defer();
ctx.then(function (context) {
deferred.resolve(bctx.filter(parse(context), filter));
});
compiled.push(deferred.promise);
} else {
ctx = bctx.filter(parse(ctx), filter);
if (!empty(ctx)) compiled.push(ctx);
}
return compiled;
});
}
Expand All @@ -76,7 +86,8 @@ buster.testContext = (function () {
return acc;
}

return { compile: compile, filter: filter };
bctx = { compile: compile, filter: filter };
return bctx;
}());

if (typeof module == "object") {
Expand Down
13 changes: 13 additions & 0 deletions test/unit/buster-test/test-context-test.js
Expand Up @@ -262,5 +262,18 @@ buster.util.testCase("ContextFilterTest", {

assert.equals(contexts.length, 1);
assert.equals(contexts[0].tests.length, 2);
},

"should return promise for promise input": function () {
var context = buster.testCase("Some tests", {
"test 1": function () {},
"test 2": function () {},
"should be dropped": function () {}
});
var promise = when(context);
var contexts = buster.testContext.compile([promise], "test ");

assert.equals(contexts.length, 1);
assert.isFunction(contexts[0].then);
}
});

0 comments on commit 98cc7e4

Please sign in to comment.