From e599ff107a2a0b7bea8f501382144a54f51df91a Mon Sep 17 00:00:00 2001 From: Julian Grinblat Date: Mon, 14 Oct 2024 12:27:59 +0900 Subject: [PATCH 1/4] feat(suite): allow calling hook methods --- lib/suite.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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; }; /** From 7948b1dc863d54999d7720d9ee9eb2b221ff5273 Mon Sep 17 00:00:00 2001 From: Julian Grinblat Date: Mon, 14 Oct 2024 12:56:50 +0900 Subject: [PATCH 2/4] add tests and docs --- docs/index.md | 11 +++++++++++ test/unit/timeout.spec.js | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) 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/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); + }); }); }); From 2b4b3c1869731cc7447449cca1c4b062e9c9a564 Mon Sep 17 00:00:00 2001 From: Julian Grinblat Date: Wed, 30 Oct 2024 23:04:59 +0900 Subject: [PATCH 3/4] fix --- lib/interfaces/common.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/interfaces/common.js b/lib/interfaces/common.js index ba8b5f85ed..d6656b8cc6 100644 --- a/lib/interfaces/common.js +++ b/lib/interfaces/common.js @@ -57,7 +57,7 @@ module.exports = function (suites, context, mocha) { * @param {Function} fn */ before: function (name, fn) { - suites[0].beforeAll(name, fn); + return suites[0].beforeAll(name, fn); }, /** @@ -67,7 +67,7 @@ module.exports = function (suites, context, mocha) { * @param {Function} fn */ after: function (name, fn) { - suites[0].afterAll(name, fn); + return suites[0].afterAll(name, fn); }, /** @@ -77,7 +77,7 @@ module.exports = function (suites, context, mocha) { * @param {Function} fn */ beforeEach: function (name, fn) { - suites[0].beforeEach(name, fn); + return suites[0].beforeEach(name, fn); }, /** @@ -87,7 +87,7 @@ module.exports = function (suites, context, mocha) { * @param {Function} fn */ afterEach: function (name, fn) { - suites[0].afterEach(name, fn); + return suites[0].afterEach(name, fn); }, suite: { From aae63ffbf19adc6a390008b005a1e8a16888c133 Mon Sep 17 00:00:00 2001 From: Julian Grinblat Date: Thu, 31 Oct 2024 01:29:41 +0900 Subject: [PATCH 4/4] address comment --- docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index a3d1847c7f..cb14bb52b2 100644 --- a/docs/index.md +++ b/docs/index.md @@ -341,7 +341,7 @@ 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); }).timeout(1000); ```