-
Notifications
You must be signed in to change notification settings - Fork 1.4k
add after.always
and afterEach.always
hooks - fixes #474
#806
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
Changes from 4 commits
bee703f
7694d80
ae51391
c99e958
b40c7a8
2270406
f90bc9a
c482777
c38eaff
43e4d73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,9 @@ function TestCollection() { | |
before: [], | ||
beforeEach: [], | ||
after: [], | ||
afterEach: [] | ||
afterAlways: [], | ||
afterEach: [], | ||
afterEachAlways: [] | ||
}; | ||
|
||
this._emitTestResult = this._emitTestResult.bind(this); | ||
|
@@ -64,7 +66,15 @@ TestCollection.prototype.add = function (test) { | |
throw new Error('"only" cannot be used with a ' + type + ' test'); | ||
} | ||
|
||
this.hooks[type].push(test); | ||
// add a always hook | ||
if (metadata.always) { | ||
if (type !== 'after' && type !== 'afterEach') { | ||
throw new Error('"always" cannot be used with a ' + type + ' test'); | ||
} | ||
this.hooks[type + 'Always'].push(test); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, a few tests in |
||
} else { | ||
this.hooks[type].push(test); | ||
} | ||
return; | ||
} | ||
|
||
|
@@ -151,20 +161,25 @@ TestCollection.prototype._buildTest = function (test, context) { | |
|
||
TestCollection.prototype._buildTestWithHooks = function (test) { | ||
if (test.metadata.skipped) { | ||
return [this._skippedTest(this._buildTest(test))]; | ||
return new Sequence([this._skippedTest(this._buildTest(test))], true); | ||
} | ||
|
||
var context = {context: {}}; | ||
|
||
var beforeHooks = this._buildHooks(this.hooks.beforeEach, test.title, context); | ||
var afterHooks = this._buildHooks(this.hooks.afterEach, test.title, context); | ||
|
||
return [].concat(beforeHooks, this._buildTest(test, context), afterHooks); | ||
var sequence = new Sequence([].concat(beforeHooks, this._buildTest(test, context), afterHooks), true); | ||
if (this.hooks.afterEachAlways.length !== 0) { | ||
var afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterEachAlways, test.title, context)); | ||
sequence = new Sequence([sequence, afterAlwaysHooks], false); | ||
} | ||
return sequence; | ||
}; | ||
|
||
TestCollection.prototype._buildTests = function (tests) { | ||
return tests.map(function (test) { | ||
return new Sequence(this._buildTestWithHooks(test), true); | ||
return this._buildTestWithHooks(test); | ||
}, this); | ||
}; | ||
|
||
|
@@ -176,5 +191,10 @@ TestCollection.prototype.build = function (bail) { | |
var concurrentTests = new Concurrent(this._buildTests(this.tests.concurrent), bail); | ||
var allTests = new Sequence([serialTests, concurrentTests]); | ||
|
||
return new Sequence([beforeHooks, allTests, afterHooks], true); | ||
var finalTests = new Sequence([beforeHooks, allTests, afterHooks], true); | ||
if (this.hooks.afterAlways.length !== 0) { | ||
var afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterAlways)); | ||
finalTests = new Sequence([finalTests, afterAlwaysHooks], false); | ||
} | ||
return finalTests; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -418,9 +418,9 @@ test.todo('will think about writing this later'); | |
|
||
AVA lets you register hooks that are run before and after your tests. This allows you to run setup and/or teardown code. | ||
|
||
`test.before()` registers a hook to be run before the first test in your test file. Similarly `test.after()` registers a hook to be run after the last test. | ||
`test.before()` registers a hook to be run before the first test in your test file. Similarly `test.after()` registers a hook to be run after the last test. `test.after.always()` registers a hook to be run **always** after the last test, even if before hook/some tests fail. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
`test.beforeEach()` registers a hook to be run before each test in your test file. Similarly `test.afterEach()` a hook to be run after each test. | ||
`test.beforeEach()` registers a hook to be run before each test in your test file. Similarly `test.afterEach()` a hook to be run after each test. `test.afterEach.always()` registers a hook to be run **always** after each test, even if beforeEach hook/the test fails. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what @novemberborn said, plus:
|
||
|
||
Like `test()` these methods take an optional title and a callback function. The title is shown if your hook fails to execute. The callback is called with an [execution object](#t). | ||
|
||
|
@@ -439,6 +439,10 @@ test.after('cleanup', t => { | |
// this runs after all tests | ||
}); | ||
|
||
test.after.always('always cleanup', t => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// this runs always after all tests | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); | ||
|
||
test.beforeEach(t => { | ||
// this runs before each test | ||
}); | ||
|
@@ -447,6 +451,10 @@ test.afterEach(t => { | |
// this runs after each test | ||
}); | ||
|
||
test.afterEach.always(t => { | ||
// this runs always after each test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps:
|
||
}); | ||
|
||
test(t => { | ||
// regular test | ||
}); | ||
|
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.
@cgcgbcbc - Can you add a few tests in
test/test-collection.js
for this throwing behavior?