Skip to content

Commit 1b176a0

Browse files
yaronassacharlierudolph
authored andcommitted
fix unhandled rejections in handlers (#798)
resolves #792
1 parent 7661a03 commit 1b176a0

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

features/hooks.feature

+47
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,53 @@ Feature: Environment Hooks
253253
]
254254
"""
255255

256+
Scenario: Failing the BeforeFeatures hook exists immediately with code = 1
257+
Given a file named "features/a.feature" with:
258+
"""
259+
Feature: some feature
260+
261+
Scenario: I've declared one step and it is passing
262+
Given This step is passing
263+
"""
264+
And a file named "features/step_definitions/cucumber_steps.js" with:
265+
"""
266+
var cucumberSteps = function() {
267+
this.Given(/^This step is passing$/, function(callback) { console.log('is passing'); callback(); });
268+
};
269+
module.exports = cucumberSteps;
270+
"""
271+
And a file named "features/support/hooks.js" with:
272+
"""
273+
var hooks = function () {
274+
this.registerHandler('BeforeFeatures', function(){
275+
return {
276+
then: function(resolve, reject){
277+
setTimeout(function(){
278+
reject(new Error('Something bad'))
279+
});
280+
}
281+
};
282+
});
283+
};
284+
285+
module.exports = hooks;
286+
"""
287+
When I run cucumber.js with `-f json`
288+
Then the exit status should be non-zero
289+
And the error output contains the text:
290+
"""
291+
features/support/hooks.js:2 Something bad
292+
"""
293+
And the output does not contain the text:
294+
"""
295+
is passing
296+
"""
297+
And the error output does not contain the text:
298+
"""
299+
is passing
300+
"""
301+
302+
256303
Scenario: Hooks still execute after a failure
257304
Given a file named "features/a.feature" with:
258305
"""

features/step_definitions/cli_steps.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,21 @@ var cliSteps = function cliSteps() {
6363
getAdditionalErrorText(this.lastRun));
6464
});
6565

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

6970
actualOutput = normalizeText(actualOutput);
7071
expectedOutput = normalizeText(expectedOutput);
7172

72-
if (actualOutput.indexOf(expectedOutput) === -1)
73-
throw new Error('Expected output to contain the following:\n' + expectedOutput + '\n' +
74-
'Got:\n' + actualOutput + '\n' +
75-
getAdditionalErrorText(this.lastRun));
73+
if ((actualOutput.indexOf(expectedOutput) !== -1) !== expectedToFind) {
74+
var errorPhrase = (expectedToFind) ? 'Expected output to contain the following:' : 'Expected output not to contain the following:';
75+
76+
throw new Error(errorPhrase + '\n' + expectedOutput + '\n' +
77+
'Got:\n' + actualOutput + '\n' +
78+
getAdditionalErrorText(this.lastRun));
79+
}
80+
7681
});
7782

7883
this.Then(/^I see the version of Cucumber$/, function() {

lib/cucumber/runtime/event_broadcaster.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function EventBroadcaster(listeners, listenerDefaultTimeout) {
2727
Cucumber.Util.asyncForEach(listeners, function (listener, callback) {
2828
listener.hear(event, listenerDefaultTimeout, function(error) {
2929
if (error) {
30-
throw error;
30+
process.nextTick(function(){ throw error; }); // prevent swallow by unhandled rejection
3131
}
3232
callback();
3333
});

0 commit comments

Comments
 (0)