-
Notifications
You must be signed in to change notification settings - Fork 779
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
Core: Introduce before/after hooks for modules #919
Changes from all commits
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 |
---|---|---|
|
@@ -75,8 +75,10 @@ Test.prototype = { | |
config.current = this; | ||
|
||
if ( this.module.testEnvironment ) { | ||
delete this.module.testEnvironment.before; | ||
delete this.module.testEnvironment.beforeEach; | ||
delete this.module.testEnvironment.afterEach; | ||
delete this.module.testEnvironment.after; | ||
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 may require treating the change as breaking. @leobalter? 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. I don't think so. We have this delete statement only because we're introducing new properties to the modules' hook: That same could happen when we moved 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.
Exactly. Code like Anyway, while maybe not a huge deal, this is technically breaking. 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.
We should discuss a way to solve this before 2.0. Maybe we need to isolate the hooks from the environment or event remove the possibility to define env values on this object (let |
||
} | ||
this.testEnvironment = extend( {}, this.module.testEnvironment ); | ||
|
||
|
@@ -133,10 +135,22 @@ Test.prototype = { | |
checkPollution(); | ||
}, | ||
|
||
queueHook: function( hook, hookName ) { | ||
queueHook: function( hook, hookName, hookOwner ) { | ||
var promise, | ||
test = this; | ||
return function runHook() { | ||
if ( hookName === "before" ) { | ||
if ( hookOwner.testsRun !== 0 ) { | ||
return; | ||
} | ||
|
||
test.preserveEnvironment = true; | ||
} | ||
|
||
if ( hookName === "after" && hookOwner.testsRun !== numberOfTests( hookOwner ) - 1 ) { | ||
return; | ||
} | ||
|
||
config.current = test; | ||
if ( config.notrycatch ) { | ||
callHook(); | ||
|
@@ -166,7 +180,7 @@ Test.prototype = { | |
} | ||
if ( module.testEnvironment && | ||
QUnit.objectType( module.testEnvironment[ handler ] ) === "function" ) { | ||
hooks.push( test.queueHook( module.testEnvironment[ handler ], handler ) ); | ||
hooks.push( test.queueHook( module.testEnvironment[ handler ], handler, module ) ); | ||
} | ||
} | ||
|
||
|
@@ -205,6 +219,7 @@ Test.prototype = { | |
} | ||
} | ||
|
||
notifyTestsRan( this.module ); | ||
runLoggingCallbacks( "testDone", { | ||
name: this.testName, | ||
module: this.module.name, | ||
|
@@ -233,6 +248,13 @@ Test.prototype = { | |
config.current = undefined; | ||
}, | ||
|
||
preserveTestEnvironment: function() { | ||
if ( this.preserveEnvironment ) { | ||
this.module.testEnvironment = this.testEnvironment; | ||
this.testEnvironment = extend( {}, this.module.testEnvironment ); | ||
} | ||
}, | ||
|
||
queue: function() { | ||
var priority, | ||
test = this; | ||
|
@@ -249,16 +271,25 @@ Test.prototype = { | |
test.before(); | ||
}, | ||
|
||
test.hooks( "before" ), | ||
|
||
function() { | ||
test.preserveTestEnvironment(); | ||
}, | ||
|
||
test.hooks( "beforeEach" ), | ||
|
||
function() { | ||
test.run(); | ||
}, | ||
|
||
test.hooks( "afterEach" ).reverse(), | ||
test.hooks( "after" ).reverse(), | ||
|
||
function() { | ||
test.after(); | ||
}, | ||
|
||
function() { | ||
test.finish(); | ||
} | ||
|
@@ -645,3 +676,18 @@ function only( testName, expected, callback, async ) { | |
|
||
newTest.queue(); | ||
} | ||
|
||
function numberOfTests( module ) { | ||
var count = module.tests.length; | ||
while ( module = module.childModule ) { | ||
count += module.tests.length; | ||
} | ||
return count; | ||
} | ||
|
||
function notifyTestsRan( module ) { | ||
module.testsRun++; | ||
while ( module = module.parentModule ) { | ||
module.testsRun++; | ||
} | ||
} |
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.
this might be my only concern on this PR. Exposing a new detail on the API conflicts with the plans for the EventEmitter changes (#882).
Although, the
tests
array will still prevail and I wonder if we can set an equivalent property on the reference object for the test on that array. Doing that, we would have the tradeoff to filter the array, but it would save us a new API exposure.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.
Looking at the "events details types" issue, I think we would be able to replace
module.testsRun
withmodule.status.total
when it is implemented since it looks like it will be similar.I would also be fine with setting an equivalent property on the
tests
array, though it feels a little less correct.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.
👍 that would be great!
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.
In the interim, should we proceed with this current implementation? (After I clean up the style issues, of course.)
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.
Yes, sure.
We will need to have the API docs on http://github.com/jquery/api.qunitjs.com to be able to land it, would you mind to help on that part too?
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'll open a PR documenting the new
before
/after
hooks formodule
. Is there anything else I should document?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 believe they'll be enough for now, QUnit docs will probably face a whole logging details review on a near future.