Skip to content

Commit

Permalink
Adds support for async tests, though not in ideal fashion due to moch…
Browse files Browse the repository at this point in the history
…a lacking support for async skip (mochajs/mocha#1625)
  • Loading branch information
dmarcelino committed May 30, 2015
1 parent d6979af commit 01314d3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
52 changes: 36 additions & 16 deletions it.optional.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Expose `TestOptional`.
*/
exports = module.exports = TestOptional;
var tryasync = require('./tryasync');

/**
* Initialize a new `Test` with the given `title` and callback `fn`, if test
Expand All @@ -14,23 +15,42 @@ exports = module.exports = TestOptional;
function TestOptional(title, titlePending, fn) {
if (!fn) {
fn = titlePending;
titlePending = 'Pending: ' + title;
titlePending = 'PENDING: ' + title;
}

function processResult(result) {
if (result) {
it(titlePending);
} else {
it(title, function() {});

it(title, function(done){
var self = this;

function processResult(result) {
if (result) {
self.skip();
}
done();
};

function executeTest(){
if (fn.length === 0) {
processResult(fn());
} else {
fn.call(self, processResult);
}
}
};

try {
if (fn.length === 0) {
return processResult(fn());

function handleErrorAsync(e){
self.test.title = titlePending;
// async skip not supported yet: https://github.com/mochajs/mocha/issues/1625
// self.skip();
done();
}
fn(processResult);
} catch (e) {
it(titlePending);
}

try {
tryasync(executeTest)
.catch(handleErrorAsync);
} catch(e){
// sync'ed / same tick exception
self.test.title = titlePending;
self.skip();
}

});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "it-optional",
"version": "0.0.1",
"version": "0.0.2",
"description": "Mocha add-on method it.optional() which marks a test as pending if it would fail",
"main": "it.optional.js",
"directories": {
Expand Down
8 changes: 7 additions & 1 deletion test/it.optional.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ describe('optional tests', function(){
done(new Error('oops'));
});

it.optional('should do mno eventually', function(done){
setTimeout(function(){
done(new Error('oops'));
}, 5);
});

it.optional('should do ikj eventually', function(done){
throw new Error('uncaught error');
});

it.optional('should do abc eventually', function(done){
setTimeout(function(){
assert.equal('a', 'b');
}, 100);
}, 5);
});

});
Expand Down
17 changes: 17 additions & 0 deletions tryasync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// From http://stackoverflow.com/questions/14301839/javascript-asynchronous-exception-handling-with-node-js

var domain = require("domain");
module.exports = function (func) {
var F = function () { };
var dom = domain.create();
F.prototype.catch = function (errHandle) {
var args = arguments;
dom.on("error", function (err) {
return errHandle(err, dom);
}).run(function () {
func.call(null, args);
});
return this;
};
return new F();
};

0 comments on commit 01314d3

Please sign in to comment.