Skip to content

Instantly share code, notes, and snippets.

@shyiko
Created May 18, 2014 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shyiko/6ca40d0733afa09f7699 to your computer and use it in GitHub Desktop.
Save shyiko/6ca40d0733afa09f7699 to your computer and use it in GitHub Desktop.
Monkey patch bringing Mocha's --grep and --invert to Cucumber.js
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;
};
}
};
@shyiko
Copy link
Author

shyiko commented May 18, 2014

Usage:

cucumber-js --require <path>/grep.js --grep:"part of the scenario name" --require test/features/ test/features/

@shyiko
Copy link
Author

shyiko commented May 18, 2014

Another option would be to override step.acceptVisitor on BeforeScenario event (which is a much better approach really).

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