From 98cc7e43d01b1d047003c43f672f4d0006ff9f3d Mon Sep 17 00:00:00 2001 From: Christian Johansen Date: Mon, 9 Apr 2012 00:39:51 +0200 Subject: [PATCH] testContext.compile is now promise-aware - 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 --- lib/buster-test/test-context.js | 21 ++++++++++++++++----- test/unit/buster-test/test-context-test.js | 13 +++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/buster-test/test-context.js b/lib/buster-test/test-context.js index 3a1db22..3eda7cb 100644 --- a/lib/buster-test/test-context.js +++ b/lib/buster-test/test-context.js @@ -1,8 +1,11 @@ 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; @@ -10,7 +13,7 @@ buster.testContext = (function () { 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); } @@ -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; }); } @@ -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") { diff --git a/test/unit/buster-test/test-context-test.js b/test/unit/buster-test/test-context-test.js index fad7078..ca0571b 100644 --- a/test/unit/buster-test/test-context-test.js +++ b/test/unit/buster-test/test-context-test.js @@ -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); } });