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

Resolve unhandled rejection in BeforeFeatures hook #797 #798

Merged
merged 9 commits into from
Apr 24, 2017
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
47 changes: 47 additions & 0 deletions features/hooks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,53 @@ 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) { console.log('is passing'); callback(); });
};
module.exports = cucumberSteps;
"""
And a file named "features/support/hooks.js" with:
"""
var hooks = function () {
this.registerHandler('BeforeFeatures', function(){
return {
then: function(resolve, reject){
setTimeout(function(){
reject(new Error('Something bad'))
});
}
};
});
};

module.exports = hooks;
"""
When I run cucumber.js with `-f json`
Then the exit status should be non-zero
And the error output contains the text:
"""
features/support/hooks.js:2 Something bad
"""
And the output does not contain the text:
"""
is passing
"""
And the error output does not contain the text:
"""
is passing
"""


Scenario: Hooks still execute after a failure
Given a file named "features/a.feature" with:
"""
Expand Down
15 changes: 10 additions & 5 deletions features/step_definitions/cli_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,21 @@ var cliSteps = function cliSteps() {
getAdditionalErrorText(this.lastRun));
});

this.Then(/^the (error )?output contains the text:$/, function(error, expectedOutput) {
this.Then(/^the (error )?output (does not)? ?contains? the text:$/, function(error, shouldFind, expectedOutput) {
var expectedToFind = !(shouldFind);
var actualOutput = error ? this.lastRun.stderr : this.lastRun.stdout;

actualOutput = normalizeText(actualOutput);
expectedOutput = normalizeText(expectedOutput);

if (actualOutput.indexOf(expectedOutput) === -1)
throw new Error('Expected output to contain the following:\n' + expectedOutput + '\n' +
'Got:\n' + actualOutput + '\n' +
getAdditionalErrorText(this.lastRun));
if ((actualOutput.indexOf(expectedOutput) !== -1) !== expectedToFind) {
var errorPhrase = (expectedToFind) ? 'Expected output to contain the following:' : 'Expected output not to contain the following:';

throw new Error(errorPhrase + '\n' + expectedOutput + '\n' +
'Got:\n' + actualOutput + '\n' +
getAdditionalErrorText(this.lastRun));
}

});

this.Then(/^I see the version of Cucumber$/, function() {
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