Skip to content

Commit

Permalink
feat(rules): Add option --time-setup to include beforeEach in duration
Browse files Browse the repository at this point in the history
Fix timing to work with latest version of mocha. Add tests.

Fixes #419
  • Loading branch information
sgilroy committed Feb 27, 2019
1 parent 1cff7c6 commit b438874
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/cli/run-option-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ exports.types = {
'recursive',
'reporters',
'sort',
'time-setup',
'watch'
],
number: ['retries', 'slow', 'timeout'],
Expand Down
5 changes: 5 additions & 0 deletions lib/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ exports.builder = yargs =>
description: 'Specify test timeout threshold (in milliseconds)',
group: GROUPS.RULES
},
'time-setup': {
description:
'Include any beforeEach (or setup) time in duration of each test',
group: GROUPS.RULES
},
ui: {
default: defaults.ui,
description: 'Specify user interface',
Expand Down
16 changes: 16 additions & 0 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function Mocha(options) {
.reporter(options.reporter, options.reporterOptions)
.useColors(options.color)
.slow(options.slow)
.timeSetup(options.timeSetup)
.useInlineDiffs(options.inlineDiffs)
.globals(options.globals);

Expand Down Expand Up @@ -666,6 +667,21 @@ Mocha.prototype.slow = function(msecs) {
return this;
};

/**
* Sets time setup option, which will cause time for any beforeEach (setup)
* methods to be included in the duration of each test.
*
* @public
* @see {@link https://mochajs.org/#-s---time-setup|CLI option}
* @param {boolean} timeSetup - Whether to enable the time setup option.
* @return {Mocha} this
* @chainable
*/
Mocha.prototype.timeSetup = function(timeSetup) {
this.suite.timeSetup(timeSetup);
return this;
};

/**
* Enables or disables timeouts.
*
Expand Down
4 changes: 2 additions & 2 deletions lib/runnable.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ Runnable.prototype.globals = function(globals) {
Runnable.prototype.run = function(fn) {
var self = this;
var start;
if (typeof self.start === 'number') {
if (self.start) {
start = self.start;
} else {
start = Date.now();
start = new Date();
}
var ctx = this.ctx;
var finished;
Expand Down
13 changes: 11 additions & 2 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ var type = utils.type;
var createInvalidExceptionError = require('./errors')
.createInvalidExceptionError;

/**
* Save timer references to avoid Sinon interfering (see GH-237).
*/
var Date = global.Date;

/**
* Non-enumerable globals.
* @readonly
Expand Down Expand Up @@ -501,13 +506,15 @@ Runner.prototype.parents = function() {
Runner.prototype.runTest = function(fn) {
var self = this;
var test = this.test;
test.start = self.start;

if (!test) {
return;
}

var suite = this.parents().reverse()[0] || this.suite;
if (suite._timeSetup) {
test.start = self.start;
}
if (this.forbidOnly && suite.hasOnly()) {
fn(new Error('`.only` forbidden'));
return;
Expand Down Expand Up @@ -624,7 +631,9 @@ Runner.prototype.runTests = function(suite, fn) {
}

// execute test and hook(s)
self.start = Date.now();
if (suite._timeSetup) {
self.start = new Date();
}
self.emit(constants.EVENT_TEST_BEGIN, (self.test = test));
self.hookDown(HOOK_TYPE_BEFORE_EACH, function(err, errSuite) {
if (test.isPending()) {
Expand Down
19 changes: 19 additions & 0 deletions lib/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function Suite(title, parentContext, isRoot) {
this._timeout = 2000;
this._enableTimeouts = true;
this._slow = 75;
this._timeSetup = false;
this._bail = false;
this._retries = -1;
this._onlyTests = [];
Expand Down Expand Up @@ -107,6 +108,7 @@ Suite.prototype.clone = function() {
suite.enableTimeouts(this.enableTimeouts());
suite.slow(this.slow());
suite.bail(this.bail());
suite.timeSetup(this.timeSetup());
return suite;
};

Expand Down Expand Up @@ -183,6 +185,22 @@ Suite.prototype.slow = function(ms) {
return this;
};

/**
* Set or get time setup option.
*
* @private
* @param {boolean} timeSetup
* @return {Suite|boolean} for chaining
*/
Suite.prototype.timeSetup = function(timeSetup) {
if (!arguments.length) {
return this._timeSetup;
}
debug('timeSetup %s', timeSetup);
this._timeSetup = timeSetup;
return this;
};

/**
* Set or get whether to bail after first error.
*
Expand Down Expand Up @@ -338,6 +356,7 @@ Suite.prototype.addSuite = function(suite) {
suite.enableTimeouts(this.enableTimeouts());
suite.slow(this.slow());
suite.bail(this.bail());
suite.timeSetup(this.timeSetup());
this.suites.push(suite);
this.emit(constants.EVENT_SUITE_ADD_SUITE, suite);
return this;
Expand Down
10 changes: 10 additions & 0 deletions test/integration/fixtures/options/time-setup.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

describe('time-setup', function () {
beforeEach(function(done) {
// simulate some slow setup process
setTimeout(done, 20);
});

it('should run quickly', function () {});
});
39 changes: 39 additions & 0 deletions test/integration/options/timeSetup.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

var path = require('path').posix;
var helpers = require('../helpers');
var runMochaJSON = helpers.runMochaJSON;

describe('--time-setup', function() {
var args = [];

describe('with the option', function() {
before(function() {
args = ['--time-setup'];
});

it('should include the setup time', function(done) {
var fixture = path.join('options', 'time-setup');
runMochaJSON(fixture, args, function(err, res) {
if (err) {
return done(err);
}

expect(res.tests[0].duration, 'to be greater than or equal to', 20);
done();
});
});
});

it('should not include the setup time if --time-setup is not specified', function(done) {
var fixture = path.join('options', 'time-setup');
runMochaJSON(fixture, args, function(err, res) {
if (err) {
return done(err);
}

expect(res.tests[0].duration, 'to be less than', 20);
done();
});
});
});

0 comments on commit b438874

Please sign in to comment.