Skip to content

Commit

Permalink
append filepath to "timeout exceeded" exception; closes #627- all `Ru…
Browse files Browse the repository at this point in the history
…nnable`s *should* now have a `file` property- filepath is appended to the `Error` message in parens- DRY-style refactors
  • Loading branch information
boneskull committed Mar 22, 2018
1 parent 424ef84 commit 3633fa0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
21 changes: 17 additions & 4 deletions lib/runnable.js
Expand Up @@ -252,8 +252,7 @@ Runnable.prototype.resetTimeout = function () {
if (!self._enableTimeouts) {
return;
}
self.callback(new Error('Timeout of ' + ms +
'ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.'));
self.callback(self._timeoutError(ms));
self.timedOut = true;
}, ms);
};
Expand Down Expand Up @@ -319,8 +318,7 @@ Runnable.prototype.run = function (fn) {
self.duration = new Date() - start;
finished = true;
if (!err && self.duration > ms && self._enableTimeouts) {
err = new Error('Timeout of ' + ms +
'ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.');
err = self._timeoutError(ms);
}
fn(err);
}
Expand Down Expand Up @@ -417,3 +415,18 @@ Runnable.prototype.run = function (fn) {
});
}
};

/**
* Instantiates a "timeout" error
*
* @param {number} ms - Timeout (in milliseconds)
* @returns {Error} a "timeout" error
* @private
*/
Runnable.prototype._timeoutError = function (ms) {
var msg = 'Timeout of ' + ms + 'ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.';
if (this.file) {
msg += ' (' + this.file + ')';
}
return new Error(msg);
};
51 changes: 23 additions & 28 deletions lib/suite.js
Expand Up @@ -189,6 +189,25 @@ Suite.prototype.isPending = function () {
return this.pending || (this.parent && this.parent.isPending());
};

/**
* Generic hook-creator.
* @private
* @param {string} title - Title of hook
* @param {Function} fn - Hook callback
* @returns {Hook} A new hook
*/
Suite.prototype._createHook = function (title, fn) {
var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
hook.retries(this.retries());
hook.enableTimeouts(this.enableTimeouts());
hook.slow(this.slow());
hook.ctx = this.ctx;
hook.file = this.file;
return hook;
};

/**
* Run `fn(test[, done])` before running tests.
*
Expand All @@ -207,13 +226,7 @@ Suite.prototype.beforeAll = function (title, fn) {
}
title = '"before all" hook' + (title ? ': ' + title : '');

var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
hook.retries(this.retries());
hook.enableTimeouts(this.enableTimeouts());
hook.slow(this.slow());
hook.ctx = this.ctx;
var hook = this._createHook(title, fn);
this._beforeAll.push(hook);
this.emit('beforeAll', hook);
return this;
Expand All @@ -237,13 +250,7 @@ Suite.prototype.afterAll = function (title, fn) {
}
title = '"after all" hook' + (title ? ': ' + title : '');

var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
hook.retries(this.retries());
hook.enableTimeouts(this.enableTimeouts());
hook.slow(this.slow());
hook.ctx = this.ctx;
var hook = this._createHook(title, fn);
this._afterAll.push(hook);
this.emit('afterAll', hook);
return this;
Expand All @@ -267,13 +274,7 @@ Suite.prototype.beforeEach = function (title, fn) {
}
title = '"before each" hook' + (title ? ': ' + title : '');

var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
hook.retries(this.retries());
hook.enableTimeouts(this.enableTimeouts());
hook.slow(this.slow());
hook.ctx = this.ctx;
var hook = this._createHook(title, fn);
this._beforeEach.push(hook);
this.emit('beforeEach', hook);
return this;
Expand All @@ -297,13 +298,7 @@ Suite.prototype.afterEach = function (title, fn) {
}
title = '"after each" hook' + (title ? ': ' + title : '');

var hook = new Hook(title, fn);
hook.parent = this;
hook.timeout(this.timeout());
hook.retries(this.retries());
hook.enableTimeouts(this.enableTimeouts());
hook.slow(this.slow());
hook.ctx = this.ctx;
var hook = this._createHook(title, fn);
this._afterEach.push(hook);
this.emit('afterEach', hook);
return this;
Expand Down
5 changes: 3 additions & 2 deletions test/unit/runnable.spec.js
Expand Up @@ -466,14 +466,15 @@ describe('Runnable(title, fn)', function () {
then: function () { }
};

it('should give the timeout error', function (done) {
it('should throw the timeout error', function (done) {
var test = new Runnable('foo', function () {
return foreverPendingPromise;
});
test.file = '/some/path';

test.timeout(10);
test.run(function (err) {
expect(err).to.be.ok();
expect(err.message).to.match(/Timeout of 10ms exceeded.*\(\/some\/path\)$/);
done();
});
});
Expand Down

0 comments on commit 3633fa0

Please sign in to comment.