Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
add Root Hook Plugins #4244
add Root Hook Plugins #4244
Changes from all commits
0cdb978
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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 haven't understood this part. Why you pass an object, not a function.
Suite.prototype.beforeAll = function(title, fn)
expects a function, but here it gets an object.If you run those hooks with
Runnable#run
, how can this work? The hook has already been executed and contains the return value, not the function anymore?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.
hooks
is a collection of all four types of hook, and has propertiesbeforeAll
,beforeEach
,afterAll
,afterEach
. At this point in the code, if any of these property is truthy, they are expected to be arrays of functions. I think I could be more specific about the shape of the object; before we reach this point,loadHooks()
(inrun-helpers.js
, iirc) does some pre-processing.Below, we loop thru the properties (again, which are arrays of functions) and assign them to the root suite via the appropriate method in
Suite
.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.
Do we need this typedef for typescript? Because this file is part of our bundled browser mocha.js.
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 can move it elsewhere. It's not intended to be in the public API at this point anyway. VSCode's TS language server understands it (somewhat). I just did this instead of doing it inline elsewhere, as describing more than the most trivial values in a
@param
is awkward.Given that this functionality is intended to be specific to Node.js, maybe I should move it out? That would be poor encapsulation, though. I really dislike the points in the codebase where object A is mucking with the internals of object B, and that's exactly what would have to happen if
Mocha#rootHooks()
were moved elsewhere; we'd have aMocha
instance, then some other method would need to reach intoMocha#suite
and call methods there.While this is intended to be used with Node.js, it could be used in the browser. There's no reason why a browser can't call
Mocha#rootHooks()
, but the consumer would need to pass an object adhering to theMochaRootHookObject
shape, which is awkward.Another way may be to do away with
MochaRootHookObject
entirely, and instead expose four new chainable methods:Mocha#rootBeforeAll()
,Mocha#rootBeforeEach()
,Mocha#rootAfterAll()
, andMocha#rootAfterEach()
. Each would accept a single hook function. This would probably be a more ergonomic API for a consumer (which I had not originally considered).What do you think?
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 mean, there's no reason why the browser couldn't consume
rootHooks()
. I think it's probably the right place for it for now.