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

fix timeout to allow fake timers #562

Merged
merged 6 commits into from May 30, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions features/fake_time.feature
@@ -0,0 +1,47 @@
Feature: Allow time to be faked by utilities such as sinon.useFakeTimers
Background: Before and After hooks to enable faking time.
Given a file named "features/support/hooks.js" with:
"""
var sinon = require('sinon');
var hooks = function () {

this.Before(function(scenario) {
this.clock = sinon.useFakeTimers();
});

this.After(function(scenario) {
this.clock.restore();
});
Copy link
Member

Choose a reason for hiding this comment

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

you can just omit the callback in each of these since they are synchronous. Thus you can omit scenario as well


};
module.exports = hooks;
"""

Scenario: faked time doesn't trigger the test runner timeout
Given a file named "features/passing_steps.feature" with:
"""
Feature: a feature
Scenario: a scenario
Given a faked time step
"""

Given a file named "features/step_definitions/passing_steps.js" with:
"""
var assert = require('assert');
var sinon = require('sinon')

stepDefinitions = function() {
this.Given(/^a faked time step$/, function (done) {
var testFunction = sinon.stub();
setTimeout(testFunction, 10000);
assert(!testFunction.called);
this.clock.tick(10001);
assert(testFunction.called);
done()
});
};

module.exports = stepDefinitions
"""
When I run cucumber.js with `--strict`
Then it passes
4 changes: 2 additions & 2 deletions lib/cucumber/support_code/step_definition.js
Expand Up @@ -6,7 +6,7 @@ function StepDefinition(pattern, options, code, uri, line) {
return process.hrtime();
}
else {
return new Date().getTime();
return new Cucumber.Util.RealTime.Date().getTime();
}
}

Expand All @@ -16,7 +16,7 @@ function StepDefinition(pattern, options, code, uri, line) {
return duration[0] * 1e9 + duration[1];
}
else {
return (new Date().getTime() - start) * 1e6;
return (new Cucumber.Util.RealTime.Date().getTime() - start) * 1e6;
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/cucumber/util.js
Expand Up @@ -2,6 +2,7 @@ var Util = {};
Util.Arguments = require('./util/arguments');
Util.asyncForEach = require('./util/async_for_each');
Util.Exception = require('./util/exception');
Util.RealTime = require('./util/real_time');
Util.RegExp = require('./util/reg_exp');
Util.run = require('./util/run');
Util.String = require('./util/string');
Expand Down
9 changes: 9 additions & 0 deletions lib/cucumber/util/real_time.js
@@ -0,0 +1,9 @@
module.exports = {
Date: Date,
setTimeout: setTimeout,
clearTimeout: clearTimeout,
setInterval: setInterval,
clearInterval: clearInterval,
setImmediate: setImmediate,
clearImmediate: clearImmediate
};
4 changes: 2 additions & 2 deletions lib/cucumber/util/run.js
Expand Up @@ -8,15 +8,15 @@ function run(fn, thisArg, argsArray, timeoutInMilliseconds, callback) {
function finish(error, result) {
Cucumber.Util.Exception.unregisterUncaughtExceptionHandler(finish);
if (timeoutId) {
clearTimeout(timeoutId);
Cucumber.Util.RealTime.clearTimeout(timeoutId);
}
callback(error, result);
callback = function() {};
}

argsArray.push(finish);

timeoutId = setTimeout(function(){
timeoutId = Cucumber.Util.RealTime.setTimeout(function(){
finish('function timed out after ' + timeoutInMilliseconds + ' milliseconds');
}, timeoutInMilliseconds);

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -127,6 +127,7 @@
"pogo": "^0.10.0",
"rimraf": "^2.4.3",
"serve-static": "^1.10.0",
"sinon": "^1.17.3",
"through": "^2.3.8",
"tmp": "0.0.28",
"uglifyify": "^3.0.1"
Expand Down