Skip to content

Commit

Permalink
handle empty stacktraces (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalphettes authored and charlierudolph committed Sep 18, 2016
1 parent 392b702 commit f46fa68
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
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];
}
}

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

0 comments on commit f46fa68

Please sign in to comment.