Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Optional Mocha tests: if not pass mark as pending
* Sometimes there is the need to have optional tests that may need to run conditionally as discussed in:
* This solution is good but if our tests are part of a test suite that runs against many different kinds of implementations
* it may be desirable to have optional tests that "pass" if they are supported and become "pending" if they fail.
* This way we don't have to break the whole test suit because of an optional test breaksing and, if it passes, we'll know about it.
function runOptionalTest(testName, failedMessage, testFn){
testFn = failedMessage;
failedMessage = 'WARN: ' + testName + ' is not supported';
else {
it(testName, function(){});
describe('optional tests', function(){
var successTest = function(done){
runOptionalTest('should do xyz', successTest);
var failedTest = function(done){
done(new Error('oops'));
runOptionalTest('should do xyz eventually', failedTest);
// Result:
// optional tests
// ✓ should do xyz
// - WARN: should do xyz eventually is not supported

This comment has been minimized.

Copy link
Owner Author

@dmarcelino dmarcelino commented Apr 24, 2015

I've consolidated this on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment