diff --git a/docs/index.md b/docs/index.md index b6990c4bf8..cf6010364f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -334,6 +334,17 @@ describe('my suite', () => { _If you do not need to use_ Mocha's context, lambdas should work. Be aware that using lambdas will be more painful to refactor if the need eventually arises! +Alternatively, you can override certain context variables, such as test timeouts, by chain-calling methods of the created tests and/or hooks: + +```js +describe('my suite', () => { + beforeEach(() => {}).timeout(1000); + it('my test', () => { + assert.ok(true); + }).timeout(1000); // should set the timeout of this test to 1000 ms; instead will fail +}).timeout(1000); +``` + ## Hooks With its default "BDD"-style interface, Mocha provides the hooks `before()`, `after()`, `beforeEach()`, and `afterEach()`. These should be used to set up preconditions and clean up after your tests. diff --git a/lib/suite.js b/lib/suite.js index 64d7183fdf..bce00731ec 100644 --- a/lib/suite.js +++ b/lib/suite.js @@ -257,7 +257,7 @@ Suite.prototype.beforeAll = function (title, fn) { var hook = this._createHook(title, fn); this._beforeAll.push(hook); this.emit(constants.EVENT_SUITE_ADD_HOOK_BEFORE_ALL, hook); - return this; + return hook; }; /** @@ -281,7 +281,7 @@ Suite.prototype.afterAll = function (title, fn) { var hook = this._createHook(title, fn); this._afterAll.push(hook); this.emit(constants.EVENT_SUITE_ADD_HOOK_AFTER_ALL, hook); - return this; + return hook; }; /** @@ -305,7 +305,7 @@ Suite.prototype.beforeEach = function (title, fn) { var hook = this._createHook(title, fn); this._beforeEach.push(hook); this.emit(constants.EVENT_SUITE_ADD_HOOK_BEFORE_EACH, hook); - return this; + return hook; }; /** @@ -329,7 +329,7 @@ Suite.prototype.afterEach = function (title, fn) { var hook = this._createHook(title, fn); this._afterEach.push(hook); this.emit(constants.EVENT_SUITE_ADD_HOOK_AFTER_EACH, hook); - return this; + return hook; }; /** diff --git a/test/unit/timeout.spec.js b/test/unit/timeout.spec.js index ed65156341..bb758a5e50 100644 --- a/test/unit/timeout.spec.js +++ b/test/unit/timeout.spec.js @@ -70,5 +70,31 @@ describe('timeouts', function () { }); }); }); + + describe('chaining calls', function () { + before(function (done) { + setTimeout(function () { + done(); + }, 50); + }).timeout(1500); + + it('should allow overriding via chaining', function (done) { + setTimeout(function () { + done(); + }, 50); + }).timeout(1500); + + describe('suite-level', function () { + it('should work with timeout(0)', function (done) { + setTimeout(done, 1); + }); + + describe('nested suite', function () { + it('should work with timeout(0)', function (done) { + setTimeout(done, 1); + }); + }); + }).timeout(1000); + }); }); });