Skip to content

Resolve unhandled rejection in BeforeFeatures hook #797 #798

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

Merged
merged 9 commits into from
Apr 24, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 33 additions & 0 deletions features/hooks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,39 @@ Feature: Environment Hooks
]
"""

Scenario: Failing the BeforeFeatures hook exists immediately with code = 1
Given a file named "features/a.feature" with:
"""
Feature: some feature

Scenario: I've declared one step and it is passing
Given This step is passing
"""
And a file named "features/step_definitions/cucumber_steps.js" with:
"""
var cucumberSteps = function() {
this.Given(/^This step is passing$/, function(callback) { callback(); });
};
module.exports = cucumberSteps;
"""
And a file named "features/support/hooks.js" with:
"""
var hooks = function () {
this.registerHandler('BeforeFeatures', function(){
return Promise.reject(new Error('Something bad'));
});
};

module.exports = hooks;
"""
When I run cucumber.js with `-f json`
Then the exit status should be 1
And the error output contains the text:
"""
features/support/hooks.js:2 Something bad
"""
And it does not output a valid JSON
Copy link
Member

@charlierudolph charlierudolph Mar 28, 2017

Choose a reason for hiding this comment

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

I think this step (and the related step definition) can just be removed. I don't think we gain much by having it here

Copy link
Author

@yaronassa yaronassa Mar 28, 2017

Choose a reason for hiding this comment

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

I wanted to make sure the run exits immediately without executing any scenarios.
Is there a more straightforward way to do that?

Copy link
Member

@charlierudolph charlierudolph Mar 28, 2017

Choose a reason for hiding this comment

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

I'd rather we use a step like "And it outputs no other text". The way its written now just makes we think we are somehow printing invalid json instead of printing nothing

Copy link
Author

@yaronassa yaronassa Mar 28, 2017

Choose a reason for hiding this comment

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

Yeah, you're right. I'll push an alternative that makes sure a step output isn't in stdout/stderr.


Scenario: Hooks still execute after a failure
Given a file named "features/a.feature" with:
"""
Expand Down
13 changes: 13 additions & 0 deletions features/step_definitions/json_output_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,19 @@ var jsonOutputSteps = function jsonOutputSteps() {
var features = JSON.parse(this.lastRun.stdout);
assert.equal(features[0].elements[0].description.trim(), description);
});

this.Then(/^it (does not)? ?outputs? a valid JSON$/, function(expected){
var expectedValid = (expected != 'does not');
var isValid = true;
try {
JSON.parse(this.lastRun.stdout);
} catch (e) {
isValid = false;
}


assert.equal(expectedValid, isValid);
});

};

Expand Down
2 changes: 1 addition & 1 deletion lib/cucumber/runtime/event_broadcaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function EventBroadcaster(listeners, listenerDefaultTimeout) {
Cucumber.Util.asyncForEach(listeners, function (listener, callback) {
listener.hear(event, listenerDefaultTimeout, function(error) {
if (error) {
throw error;
process.nextTick(function(){ throw error; }); // prevent swallow by unhandled rejection
}
callback();
});
Expand Down