Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(suite): allow calling hook methods #5231

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); // 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.
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);
});
});
});