-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Before/after hooks #36
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
Conversation
Hooks execute serially, before and after all tests.
…ents Before this change, code would be duplicated a lot. For example, before/after hooks require the same test run functionality as implemented in Runner#serial. But because previously Runner#serial was hard-coded to get tests from `this.tests.serial` property, it was not possible to use it to run before/after hooks. With this change, one can `this.serial([testA, testB])` to run tests.
Before this change, before hooks were treated just like regular tests. This caused unexpected behavior, when tests continued to execute, even if before hooks failed.
var before = this.tests.before; | ||
var after = this.tests.after; | ||
|
||
// TODO: refactor this bullshit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
;) Might want to do that haha.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relevant: #38
// @vdemedes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I thought about converting this.serial
and this.concurrent
to return promises, but decided postpone that. Wanted to hear feedback on a PR and thought that you might not like too much changes at once, haha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, promisification should be a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could do that after this PR gets merged (to avoid merge conflicts in commit history), no probs
Other than the two things I saw (I'm sure @sindresorhus will see something else), it looks good to me. Seems like a clean implementation. |
var before = test.before; | ||
var after = test.after; | ||
|
||
before (function (t) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
before(function (t) {
(spacing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, will change ;)
Yeah, looks good to me too, except |
9edb714
to
bab5b4f
Compare
Looks superb to me. Can you fix the merge conflict? :D |
@sindresorhus Yeah, sure, already on it ;) |
@sindresorhus You're welcome! I am really enjoying contributing to P.S. That gif is amazing :D |
Keep them coming @vdemedes :) |
Fixes #4.
Hooks are completely the same as regular tests and provide the same functionality/API.
The only thing different is when they are executed.
Changes
test.before()
to execute a test before all otherstest.after()
to execute a test after all othersRunner.prototype.concurrent
andRunner.prototype.serial
to accept array of tests, instead of getting them directly fromthis.tests.concurrent
andthis.tests.serial
properties respectivelyBehavior
After this change, here is how Ava runs tests:
Update: If at least one before hook fails, all the following tests won't be executed.
Usage
Output:

### TestsTests were added to cover new
test.before()
andtest.after()
functions.Let me know what you think!