Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.2.1 regression: dont crash if there is a single stackframe #605

Merged
merged 6 commits into from
Sep 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions features/custom_stack_trace.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Feature: Custom stack trace

Scenario: Error.prepareStackTrace override
Given a file named "features/a.feature" with:
"""
Feature: Some feature
Scenario: Some scenario
Given Error.prepareStackTrace has been overriden
"""
Given a file named "features/step_definitions/cucumber_steps.js" with:
"""
var cucumberSteps = function() {
var _prepareStackTrace = Error.prepareStackTrace;
Error.prepareStackTrace = function() {
return 'Custom message';
}

this.When(/^Error.prepareStackTrace has been overriden$/, function() {
});

Error.prepareStackTrace = _prepareStackTrace;
};
module.exports = cucumberSteps;
"""
When I run cucumber.js
Then it outputs this text:
"""
Feature: Some feature

Scenario: Some scenario
✓ Given Error.prepareStackTrace has been overriden

1 scenario (1 passed)
1 step (1 passed)
<duration-stat>
"""
And the exit status should be 0
27 changes: 18 additions & 9 deletions lib/cucumber/support_code/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ function Library(supportCodeDefinition) {
});
}

function getStackframe() {
var stackframes = StackTrace.getSync();
if (stackframes.length > 2) {
return stackframes[2];
} else {
return stackframes[0];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use lodash's _.find here instead of the manual for loop and _.includes instead of stackframes[i].fileName.indexOf.

Copy link
Member

@charlierudolph charlierudolph Aug 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually can this just be simplified to

var stackframes = StackTrace.getSync();
if (stackframes.length > 1) {
  return stackframes[1];
} else {
  return stackframes[0];
}

}

var self = {
lookupBeforeHooksByScenario: function lookupBeforeHooksByScenario(scenario) {
return self.lookupHooksByScenario(beforeHooks, scenario);
Expand Down Expand Up @@ -53,9 +62,9 @@ function Library(supportCodeDefinition) {
code = options;
options = {};
}
var stackframes = StackTrace.getSync();
var line = stackframes[1].getLineNumber();
var uri = stackframes[1].getFileName() || 'unknown';
var stackframe = getStackframe();
var line = stackframe.getLineNumber();
var uri = stackframe.getFileName() || 'unknown';
var hook = builder(code, options, uri, line);
collection.push(hook);
};
Expand All @@ -66,9 +75,9 @@ function Library(supportCodeDefinition) {
code = options;
options = {};
}
var stackframes = StackTrace.getSync();
var line = stackframes[1].getLineNumber();
var uri = stackframes[1].getFileName() || 'unknown';
var stackframe = getStackframe();
var line = stackframe.getLineNumber();
var uri = stackframe.getFileName() || 'unknown';
var stepDefinition = Cucumber.SupportCode.StepDefinition(name, options, code, uri, line);
stepDefinitions.push(stepDefinition);
},
Expand All @@ -82,9 +91,9 @@ function Library(supportCodeDefinition) {
handler = options;
options = {};
}
var stackframes = StackTrace.getSync();
options.line = stackframes[1].getLineNumber();
options.uri = stackframes[1].getFileName() || 'unknown';
var stackframe = getStackframe();
options.line = stackframe.getLineNumber();
options.uri = stackframe.getFileName() || 'unknown';
var listener = Cucumber.Listener(options);
listener.setHandlerForEvent(eventName, handler);
self.registerListener(listener);
Expand Down