Created
May 18, 2014 12:57
-
-
Save shyiko/6ca40d0733afa09f7699 to your computer and use it in GitHub Desktop.
Monkey patch bringing Mocha's --grep and --invert to Cucumber.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function unquote(value) { | |
return /^'.*'|".*"$/.test(value) ? value.slice(1, -1) : value; | |
} | |
module.exports = function (options) { | |
process.argv.reduce(function (obj, value) { | |
var groups = /^--(\w+)(?::(.+))?$/.exec(value); | |
if (groups !== null) { | |
obj[groups[1]] = unquote(groups[2] || ''); | |
} | |
return obj; | |
}, options || (options = {})); | |
var normalize = typeof options.normalize === 'function' ? options.normalize : | |
function (value) { | |
return value.toLowerCase(); | |
}; | |
if (options.grep) { | |
var grep = normalize(options.grep); | |
var invert = options.invert === ''; | |
var runtimeModuleId = Object.keys(require.cache).filter(function (moduleId) { | |
return moduleId.indexOf('cucumber/lib/cucumber/runtime.js') > 0; | |
}); | |
var Runtime = require.cache[runtimeModuleId[0]].exports; | |
var OriginalAstTreeWalker = Runtime.AstTreeWalker; | |
Runtime.AstTreeWalker = function () { | |
var result = OriginalAstTreeWalker.apply(this, arguments); | |
var originalVisitScenario = result.visitScenario; | |
result.visitScenario = function (scenario, callback) { | |
var match = !!~normalize(scenario.getName()).indexOf(grep); | |
if (match ^ invert) { | |
originalVisitScenario.apply(this, arguments); | |
} else { | |
callback(); | |
} | |
}; | |
return result; | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another option would be to override step.acceptVisitor on BeforeScenario event (which is a much better approach really).