Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pass scenario to hooks

Allows to determine scenario name and so make hooks more useful and
scenario-oriented.

The `scenario` argument is optional in your hook functions. Both
following examples are fine:

    this.BeforeHook(function (scenario, callback) {
      callback();
    });

    this.AfterHook(function (callback) {
      callback();
    });
  • Loading branch information...
commit 217edde758e6eb2af4e64774eb36541aa84f3549 1 parent 65d2176
@vectart vectart authored jbpros committed
View
4 features/step_definitions/cucumber_js_mappings.rb
@@ -176,7 +176,7 @@ def write_passing_hook options = {}
if hook_type == "around"
append_support_code <<-EOF
-this.#{define_hook}(#{params}function(runScenario) {
+this.#{define_hook}(#{params}function(scenario, runScenario) {
this.logCycleEvent('#{log_string}-pre');
runScenario(function(callback) {
this.logCycleEvent('#{log_string}-post');
@@ -186,7 +186,7 @@ def write_passing_hook options = {}
EOF
else
append_support_code <<-EOF
-this.#{define_hook}(#{params}function(callback) {
+this.#{define_hook}(#{params}function(scenario, callback) {
this.logCycleEvent('#{log_string}');
callback();
});
View
14 features/step_definitions/cucumber_world.js
@@ -164,10 +164,14 @@ proto.addHookWithTags = function (tags, callback) {
this._addHook({ type: "before", logEvent: "hook", tags: tags }, callback);
};
-proto._addHook = function (options, callback) {
+proto._addHook = function (options, scenario, callback) {
if (!callback) {
- callback = options;
- options = {};
+ if (scenario) {
+ callback = scenario;
+ } else {
+ callback = options;
+ options = {};
+ }
}
var type = "before";
var tags = "";
@@ -175,7 +179,7 @@ proto._addHook = function (options, callback) {
if (!options.logEvent) options.logEvent = type;
if (options.tags) tags = '"' + options.tags + '", ';
var defineHook = (type == 'before' ? 'Before' : 'After');
- this.stepDefinitions += defineHook + "(" + tags + "function(callback) {\
+ this.stepDefinitions += defineHook + "(" + tags + "function(scenario, callback) {\
world.logCycleEvent('" + options.logEvent + "');\
callback();\
});\n";
@@ -191,7 +195,7 @@ proto._addAroundHook = function (options, callback) {
var logEvent = "around";
if (options.tags) tags = '"' + options.tags + '", ';
if (options.logEvent) logEvent = options.logEvent;
- this.stepDefinitions += "Around(" + tags + "function(runScenario) {\
+ this.stepDefinitions += "Around(" + tags + "function(scenario, runScenario) {\
world.logCycleEvent('" + logEvent + "-pre');\
runScenario(function(callback) {\
world.logCycleEvent('" + logEvent + "-post');\
View
5 lib/cucumber/support_code/hook.js
@@ -8,7 +8,10 @@ var Hook = function(code, options) {
var self = {
invokeBesideScenario: function invokeBesideScenario(scenario, world, callback) {
if (self.appliesToScenario(scenario))
- code.call(world, callback);
+ if (code.length === 1)
+ code.call(world, callback);
+ else
+ code.call(world, scenario, callback);
else
callback(function(endPostScenarioAroundHook) { endPostScenarioAroundHook(); });
},
View
2  spec/cucumber/support_code/hook_spec.js
@@ -32,7 +32,7 @@ describe("Cucumber.SupportCode.Hook", function() {
it("calls the code with the world instance as this", function() {
hook.invokeBesideScenario(scenario, world, callback);
- expect(code).toHaveBeenCalledWith(callback);
+ expect(code).toHaveBeenCalledWith(scenario, callback);
expect(code.mostRecentCall.object).toBe(world);
});
Please sign in to comment.
Something went wrong with that request. Please try again.