Skip to content

Commit

Permalink
feat: allow calling hook methods (#5231)
Browse files Browse the repository at this point in the history
* feat(suite): allow calling hook methods

* add tests and docs

* fix

* address comment
  • Loading branch information
perrin4869 authored Oct 30, 2024
1 parent 05097db commit e3da641
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
11 changes: 11 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}).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.
Expand Down
8 changes: 4 additions & 4 deletions lib/interfaces/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},

/**
Expand All @@ -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);
},

/**
Expand All @@ -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);
},

/**
Expand All @@ -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: {
Expand Down
8 changes: 4 additions & 4 deletions lib/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand All @@ -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;
};

/**
Expand All @@ -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;
};

/**
Expand All @@ -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;
};

/**
Expand Down
26 changes: 26 additions & 0 deletions test/unit/timeout.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit e3da641

Please sign in to comment.