-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
global setup / teardown hooks #4308
Comments
FWIW I think it may be confusing to use |
@mochajs/core Any ideas? @jan-molak? It would be helpful to have someone with this actual use case. Might not figure this out until somebody has a problem with how it works and complains about it. 😄 |
I see a need for a begin() and end() hooks that run once for all describes in parallel mode. I'm currently doing some setup multiple times in the beforeAll() because there isn't a better option that allows me to set up once |
yes, I think this is needed, but unsure what the API should look like. open to ideas. won’t have time to implement anything for a couple weeks at least |
Been looking into how Jest does things. Setups and teardowns provided by Jest:
There is an example for getting it to work for Puppeteer (here) which uses 2 of the above. Essentially nothing I found providing a global setup and teardown for setting and sharing state between processes. There might be some libs out there which could to help us, but needs more investigation. I'll see if I can find anything on AVA. |
Thanks. I think a |
I think--but am not sure--that we should not re-run these when watching files. |
👍 This has little mental overhead and aligns with existing concepts. I was looking for something like this just today. When we look at https://mochajs.org/#run-cycle-overview that visualization I think immediately makes the point that we need a hook at a hierarchy level closer to the root of things (I looked at this picture today and wondered: why would there be no "global hook"?, then found this issue here.) The https://mochajs.org/#root-hook-plugins section was a bit repelling as of the complexity, and caveats. I spent many years writing tests in the Python ecosystem using pytest. I must say that I miss the concept of scoped "fixtures" in the JavaScript testing ecosystem. I do not like the word "fixture" too much, but let's think of it as a dependency. An individual test can require a dependency. But a test suite (test module) can, too. But also the entire test session can require/request a dependency. What we think of as "global" here would be global within a "test session" in pytest. So, in pytest, if you define a "session-scoped fixture" then the session would "consume it" (run its code), once (because there is only one session, the current session, think "test run", comprised of many test modules), and before running the first test in the first module. For testing complex systems we've always made heavy use of this concept; with quite a bit of business logic being executed as part of global test suite (well, session) setup and teardown. |
Yes, if we add something like this we will reduce the complexity there (at least in terms of documentation). If you want to run a hook ("fixture") once, use (hope that made sense) |
Looking forward to a resolution to a global setup that runs only once for all the test files. |
This will be released in v8.2.0. I do not know yet when v8.2.0 will be released. |
@boneskull Any estimate when v8.2.0 will be released? I'm trying to prioritize some work based on when this fix will be available. Thanks! |
- add "fixture flowchart wizard" to docs - added source `.sketch` file to repo - enabled skip of supporter image download in 11ty via env var `MOCHA_DOCS_SKIP_IMAGE_DOWNLOAD` for faster iteration - made the links underline on hover - Ref: #4308 Signed-off-by: Christopher Hiller <[email protected]>
With the addition of root hook plugins and parallel mode, an important use case may have been omitted.
In serial mode, a root before all (
before()
) hook can perform async setup (e.g., starting a test server) and a root after all (after()
) can perform async teardown (closing the server). This is not perfect, given Mocha could crash and the server would not get cleaned up, but I think it's behavior people expect.In parallel mode, this behavior not available. Root hooks "before all" and "after all" run once per file. If you want to run something once and only once, you're kind of stuck.
One could abuse
mochaHooks
by defining aPromise
-returning function, and doing setup there...... but there's no associated "teardown".
The only real workaround that I'm aware of is via script, e.g.,
{"scripts": {"test": "startServer && mocha; stopServer"}}
. Top-levelawait
suffers from the same problem as the above example--no associated teardown.An idea for this would be to implement two new "hooks" in
mochaHooks
, e.g.,begin
andend
.Mocha
begins running tests, similar to the behavior of the other hooksbegin
, for instance. You could definethis.foo
inbegin
and access it inend
, I suppose.Promise
-returning functions allowedAlternatives:
mochaHooks
, use a different property.mochaBegin
andmochaEnd
properties.setup
is going to be very confusing for those using thetdd
interface, for example).cc @nicojs, who may have opinions about how this should work.
The text was updated successfully, but these errors were encountered: